JavaScript prototypes and closures

Source: Internet
Author: User

A function and object of love and hatred

everything is object, but not all are objects, where undefined, number, String, Boolean) are simple value types , not objects. The rest of the situation -functions, arrays, objects, NULL, or new objects-are objects, all of which are reference types .

1. The object is a collection of attributes (important!!!!! )

everything inside an object is a property, only a property, no method --The method is also a property because its properties are represented as a key-value pair. About functions and array objects define properties that are defined in other forms.

Anyway, everything. (reference type) is an object, and the object is a collection of properties

2. Objects are function-created, functions are objects, there is a chicken egg-laying chickens logic (don't hurry back to clear this relationship)

---about objects are created by functions such as:

The nature of the above code

and and both Object and Array are functions:

Console.log (typeof (Object)); function

Console.log (typeof (Array)); function

--- function is also an object and a collection of properties

each function has a prototype property (This is why the function is an object, because it has a property in advance, and the object is a collection of attributes, the logic is not very right!). No problem! The prototype object that points to the function (since it is an object and a collection of properties!!!) ), as long as the object has a __proto__ property pointing to the constructor .

3. About objects:

I understand that if a constructor is used to create an object, the shape is to create a model, where the FN function defines a number of attributes, since FN is a function, then FN has the prototype attribute pointing to the function's prototype object. The prototype object of the function you can define some properties in it yourself, the function's prototype object has a constructor attribute pointing to the constructor. If you want to inherit these attributes from me, you can create a new object by using the New keyword.

II as soon as the new object is F1, the F1 object inherits all the properties in the function, and since F1 is an object, then there is the __proto__ attribute that points to the constructor's prototype.

III said that both object and Array are functions , because to create objects of this type to be new, they are also constructors, such as function Object () {} and Function Array () {} .。 And they are already system-created well.

IIII here, let's talk about the new mechanism for the following prototype chain to pave the way.

( here I think new not only assigns the prototype object of the constructor to the created object, but also inherits the attributes defined in the constructor )

IIIII Here is also a logic question, about the object mentioned above is the function of the creation, function is also the object, then the function is how to come out, it is impossible to jump out of the stone! And look at the following code:

4. Prototype and prototype chain in graphical form

Often see these pictures, I have a kind of impulse to kill, Impulse is the devil, understand the future will understand a lot of problems:

(The constructor is explained here, so-called constructors are functions that are used for new objects.) In fact, strictly speaking, all functions can be new, but some functions are defined for new objects, while others are not. Also note that the function name of the constructor is capitalized (rule convention) For example: Object, Array, function, etc.)

function fn () {};

var test = new FN ();

The new mechanism here is to inherit the Test.__proto__=fn.prototype and implement the prototype. To use the properties of an object, first find the property on its own, and if it is not found, find it on the prototype chain until it is empty.

5.this pointing problem

The exact value of this in the function is determined when the function is actually called and the function definition is not.

II Constructor condition 241 in the row this refers to the window when new is not new, and then points to the function function itself (also the object).

When the III function is an attribute of an object

1. If the function is a property of an object and is called as a property of an object , this in the function points to the object.

2. Note that if the FN function is not called as an attribute of obj, what will be the result?

IIII Special Cases

JavaScript has no block (The block here refers to the {})-level scope concept, and JavaScript has only functions that can create scopes except the global scope . So here's F () is a normal function that runs under the global scope.

Two problems with scopes and closures

1. And look at the following example:

Two sentences are of particular importance: 1. the variable declaration is advanced but does not assign a value of 2. function Declaration optimization and Variable declaration

Alert (a)

A (); var a=3; function A () {

Alert (10)

}

Alert (a)

c=t;

A ();

------------Split Line------------------

Alert (a)

A (); var a=3;var a=function () {

Alert (10)

}

Alert (a)

c=t;

A ();

The first question is equivalent to:

the second question is equivalent to:

      

2. Finding scope issues

here When you take the value of x, you need to take it to another scope , someone said to go to the parent scope, in fact sometimes this interpretation will be ambiguous, to create this function in the scope of the value -is "create "Instead of" call ".

What is described above is just a cross-step scope to look for.

What if it's a step that hasn't been found yet? --then cross! --continues across the global scope. If it's not found in the global scope, it's really gone.

This step-by-step "cross" route, as we call it, is a chain of scopes .

Let's summarize this "scope chain" procedure taken from a variable: (assuming a is free)

in the first step, now the current scope looks for a, if any, gets and ends. if not, continue;

The second step, if the current scope is a global scope, proves that A is undefined, ends; otherwise continues;

The third step, (not the global scope, which is the function scope), will create the scope of the function as the current scope;

Fourth step, jump to the first step.

3. Closures

as long as you know the application of two things can be:

First: function as return value

Second: The function is passed as a parameter

This is also used across scopes, and when you execute to 11 rows, look for Max to go across to the global scope when it is not found in its scope.

This article references:http://www.cnblogs.com/wangfupeng1988/p/3977924.html

Impression: This article is in late November 2017 interview summary down, the personal feeling of learning knowledge, learning technology, can not know it, do not know why, can not hold a lucky mentality.

JavaScript prototypes and closures

Related Article

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.