A few words take you to understand this, closure, prototype chain _javascript techniques in JS

Source: Internet
Author: User
Tags closure object object

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 ();}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.