📜 ⬆️ ⬇️

Handbook of JavaScript Subtleties

JavaScript Garden - Reference in English, dedicated to the subtleties of JavaScript.

From it, for example, I learned that arrays can be given length:
var list = [1, 2, 3, 4, 5];
list.length = 3;
console.log(list); // => [1, 2, 3]

What if the constructor returns an object, then new will also return this object instead of the constructed one:
var name, realCat, evilCat;
function Cat(name) {
realCat = this ;
this .name = name;
return {name: 'churchill' };
}
evilCat = new Cat( 'barsik' );
console.log(evilCat.name); // => 'churchill'
console.log(realCat.name); // => 'barsik'
console.log(name); // => undefined

What this in nested functions refers to a global object:
var name;
new function Cat() {
this .name = 'barsik' ;
( function () {
console.log( this .name); // => undefined
})();
}

That changing the arguments causes a change in the corresponding formal parameters of the function:
( function (a) {
console.log(a); // => 'a'
arguments[0] = 'b' ;
console.log(a); // => 'b'
})( 'a' );

Once again link: JavaScript Garden

')

Source: https://habr.com/ru/post/115321/


All Articles