void
operator. You may have encountered it in the form of void(0)
or void 0
. Its only purpose is to evaluate the expression to the right of it and return undefined
. 0
used here simply because it is so accepted, although it is not necessary, and any correct expression can be used here. True, this statement will return undefined
anyway. // void void 0 // undefined void (0) // undefined void 'abc' // undefined void {} // undefined void (1 === 1) // undefined void (1 !== 1) // undefined void anyfunction() // undefined
undefined
if you can just use the standard value undefined
? Isn't it true that there is some redundancy?undefined
could be assigned a new value. Let's say you could successfully execute this command: undefined = "abc"
. As a result, the value of undefined
might not be what it should be. In those days, the use of void
allowed us to provide confidence in the use of this particular undefined
. // const date = new Date() const month = new Date().getMonth() const myInstance = new MyClass() // const date = new Date const month = (new Date).getMonth() const myInstance = new MyClass
// IIFE (function () { console.log('Normal IIFE called') })() // Normal IIFE called void function () { console.log('Cool IIFE called') }() // Cool IIFE called
void
informs the parser that the code following it is a functional expression. This makes it possible to get rid of the brackets around the function declaration. And, by the way, any unary operator ( void
, +
, !
, -
, and so on) can be used here, and the code will remain working. Isn't that great? // IIFE, let result = (function () { // ... - return 'Victor Sully' })() console.log(result) // Victor Sully let result1 = function () { // ... - return 'Nathan Drake' }() console.log(result1) // Nathan Drake
with
clause in JavaScript that supports expression blocks? It looks like this: with (object) statement // with (object) { statement statement ... }
with
construction adds all the properties of the object passed to it to the chain of scopes used when executing commands. // with const person = { firstname: 'Nathan', lastname: 'Drake', age: 29 } with (person) { console.log(`${firstname} ${lastname} is ${age} years old`) } // Nathan Drake is 29 years old
with
is a great tool. It seems that it is even better than the new features of JS for the destructuring of objects , but in fact it is not.with
design is deprecated and is not recommended to be used. In strict mode, its use is prohibited. It turns out that with
blocks cause problems with performance and security.function
keyword is not the only way to define a new function. Functions can be defined dynamically using the Function
constructor and the new
operator. Here's what it looks like. // Function const multiply = new Function('x', 'y', 'return x*y') multiply(2,3) // 6
Function
constructor is the “parent” of all constructors in JavaScript. Even the Object
constructor is the Function
constructor. And its own Function
constructor is also Function
. As a result, a call of the object.constructor.constructor...
, executed for any JS object, will return the Function
constructor as a result a sufficient number of times.greet()
. We need it to display different greeting messages depending on the regional settings used. These settings can be stored in some variable external to the function. In addition, the function may have a property that defines these settings, in particular, the user's language settings. We will use the second approach. // , function greet () { if (greet.locale === 'fr') { console.log('Bonjour!') } else if (greet.locale === 'es') { console.log('Hola!') } else { console.log('Hello!') } } greet() // Hello! greet.locale = 'fr' greet() // Bonjour!
// , function generateNumber () { if (!generateNumber.counter) { generateNumber.counter = 0 } return ++generateNumber.counter } console.log(generateNumber()) // 1 console.log(generateNumber()) // 2 console.log('current counter value: ', generateNumber.counter) // current counter value: 2 generateNumber.counter = 10 console.log('current counter value: ', generateNumber.counter) // current counter value: 10 console.log(generateNumber()) // 11
arguments
object in the functions. This is a massive object, accessible within all functions (with the exception of pointer functions that do not have their own arguments
object). It contains the list of arguments passed to the function when it is called. In addition, it has some interesting properties:arguments.callee
contains a reference to the current function.arguments.caller
contains a reference to the function that called the current function. // callee caller arguments const myFunction = function () { console.log('Current function: ', arguments.callee.name) console.log('Invoked by function: ', arguments.callee.caller.name) } void function main () { myFunction() } () // Current function: myFunction // Invoked by function: main
callee
and caller
properties in strict mode, but they are still widely found in many JavaScript-compiled programs, for example, in libraries. Therefore, it is useful to know about them. // `Hello ${username}!` // myTag`Hello ${username}!`
highlight
— interprets the data of the template literal and injects this data into the finished string, placing it in the HTML <mark>
tag to highlight it when displaying such text on a web page. // function highlight(strings, ...values) { // i - let result = '' strings.forEach((str, i) => { result += str if (values[i]) { result += `<mark>${values[i]}</mark>` } }) return result } const author = 'Henry Avery' const statement = `I am a man of fortune & I must seek my fortune` const quote = highlight`${author} once said, ${statement}` // <mark>Henry Avery</mark> once said, <mark>I am a man of fortune // & I must seek my fortune</mark>
user
object, and we are trying to access its age
property using the user.age
construction. With this approach, if this property is defined, we get its value, and if not defined - we get undefined
. Everything is very simple. // const user = { firstName: 'Nathan', lastName: 'Drake', // fullname - get fullName() { return this.firstName + ' ' + this.lastName }, // set age(value) { if (isNaN(value)) throw Error('Age has to be a number') this._age = Number(value) }, get age() { return this._age } } console.log(user.fullName) // Nathan Drake user.firstName = 'Francis' console.log(user.fullName) // Francis Drake user.age = '29' console.log(user.age) // 29 // user.age = 'invalid text' // Error: Age has to be a number
let result = expression1, expression2,... expressionN
result
variable.for
loops. for (var a = 0, b = 10; a <= 10; a++, b--)
function getNextValue() { return counter++, console.log(counter), counter }
const getSquare = x => (console.log (x), x * x)
Date
objects and Moment.js library objects into timestamps. // "" +'9.11' // 9.11 +'-4' // -4 +'0xFF' // 255 +true // 1 +'123e-5' // 0.00123 +false // 0 +null // 0 +'Infinity' // Infinity +'1,234' // NaN +new Date // 1542975502981 ( ) +momentObject // 1542975502981 ( )
true
. Otherwise, it will return false
. // !!null // false !!undefined // false !!false // false !!true // true !!"" // false !!"string" // true !!0 // false !!1 // true !!{} // true !![] // true
N
get -(N+1)
. Such an expression gives 0
if N
is -1
.indexOf()
method when it checks for the existence of an element in an array or in a string, since this method does not find the element and returns -1
. // indexOf let username = "Nathan Drake" if (~username.indexOf("Drake")) { console.log('Access denied') } else { console.log('Access granted') }
includes()
. It is definitely much more convenient for determining the presence of elements than using the bitwise negation operator and indexOf()
.break
or continue
instructions. Labels can also be assigned to regular blocks of code. // declarationBlock: { // // var i, j } forLoop1: // - "forLoop1" for (i = 0; i < 3; i++) { forLoop2: // - "forLoop2" for (j = 0; j < 3; j++) { if (i === 1 && j === 1) { continue forLoop1 } console.log('i = ' + i + ', j = ' + j) } } /* i = 0, j = 0 i = 0, j = 1 i = 0, j = 2 i = 1, j = 0 i = 2, j = 0 i = 2, j = 1 i = 2, j = 2 */ // loopBlock4: { console.log('I will print') break loopBlock4 console.log('I will not print') } // I will print
goto
instruction in JS. As a result, labels are used only with the break
and continue
instructions.Source: https://habr.com/ru/post/431878/
All Articles