Prototype chain
All objects are based on the Object.prototype,object.prototype is the root object of JavaScript, the method defined in Object.prototype can be accessed by other objects, and of course can be rewritten, so directly in Object.protot Ype is called the ToString () method of the original function, which is put back to the value of the built-in property [[class]] of the Parameter object, which is a string, such as ' [Object string] '
To understand the prototype chain mechanism, you first need to know the root cause: the objects in JavaScript have a built-in property [[Prototype]], which, like non-standard __proto__ attributes, is __proto__ in ES6. It can be said that they are basically equivalent, but the built-in properties are inaccessible. objects are associated with built-in properties [[Prototype]] to form a prototype chain, and the top layer of the prototype chain is the root object Object.prototype,object.prototype the prototype will be null, i.e. Object.prototype._ _proto__ = = NULL;
For example:
When accessing the properties of an object, for example: OBJ.A, find itself first, no, find it on the object referenced by its built-in properties [[[Prototype]], or not, continue to look on the object referenced by the built-in properties [[Prototype]] of this top-level object. Always find the root object Object.prototype, can not find the return undefined;
This
The first step in understanding this is to understand that this does not point to the function itself, nor to the lexical scope of the function;
This is binding at run time, not at compile time, and its context depends on the conditions of the function call;
The binding of this has nothing to do with the location of the function declaration, only depending on where the function is invoked and how it is invoked;
This binding rule has 4 points: by priority 1 to 4
1. Called by new? Bind to the newly created empty object;
2. Called by call, apply, bind? Binding to the specified Parameter object, such as Foo.call (obj)
3. Called by context object? Bind to this context object; such as Obj.foo ()
4. Bind to global object by default, foo (), bind to undefined in strict mode;
Closed Bag
function foo () {
var a = 2;
function bar () {} return
bar;
}
var a = foo ();
Closure: not the function bar nor a, it is a reference, which is held by the internal function bar, which points to the entire scope of the external function foo, which makes the scope not be reclaimed by the garbage collector even after the external function foo () is executed. In other words, the scope of the outer function foo is the closure itself.
It holds a reference to the original defined scope, regardless of the means (directly or indirectly) passing the inner function to the lexical scope in which it is executed, and the closure is used wherever the function executes.
VAR fn; Indirect transfer function Functions
foo () {
var a = 2;
function Baz () {
console.log (a);
}
fn = baz;
}
function bar () {fn ();}
Foo ();
Bar (); function foo () {
var a = 2;
function Baz () {
console.log (a);
}
Bar (baz);
}
Function Bar (FN) {fn ();}