When a function is defined in another function, the inner function has access to the outer function’s variables.
function outerFunction() {
const outer = `I'm the outer function!`;
function innerFunction() {
const inner = `I'm the inner function!`;
console.log(outer); // I'm the outer function!
}
}
If you define any inner function within another function, this inner function is called a closure. It retains access to the variables created in the outer function.
function outerFunction() {
const outer = `I see the outer variable!`;
return function innerFunction() {
console.log(outer); // I see the outer variable!
};
}
prototype
propertynew
(eg new Name
) creates this = {}
class Example {
#priv = 0;
getPriv() {
return this.#priv;
}
}
var