JS overshadow - object
by matox
JavaScript
const book = { // A
title: 'JS for dummies',
pages: [1, 2, 3, 4, 5],
print: function() { // B
console.log(this);
this.pages.map(function(page) { // C
/*
!! Because function "C" is not part of any object it becomes part of global object
*/
console.log(this);
console.log(this.title, page);
});
}
}
book.print();
const printer = book.print; // D
printer();
/*
QUESTIONS:
- Can function "C" be called as closure? Closure is defined as function that can access values outside of own curry braces (function + lexical environment).
- I think yes, because "C" is not returned as a function.
- Why does function "C" overshadows function "B"?
- Why does this not happen if we use arrow-function?
- What happens when we "take" the function "D" out of an object? Does lexical environment change?
*/
/*
- Vsaka funkcija ima svoj this
- V primeru "D": Funkcija ob referenci nima nikoli bindan outer scope
*/