Javascript New keyword mystery and other _javascript techniques

Source: Internet
Author: User

Take a look at one of the old pros that is not fresh but interesting to the rookie:

    

What the heck is that? It's a Luan Lun.

New

With the above diagram, let's look at the second question left in the previous article, where we can add something to the constructor's function to see what happens.

  
   
   
function A () {this=1}
var=new A ()

Will get the following results:

    

Why does a A, constructed with the new keyword, get the attribute p? What does new A () do with this line of code? According to the 4th step of the creation of the function in the previous article, a object will have a construct attribute (note that the CONSTRUCTOR,CONSTURCT is not an attribute in the ECMAScript standard, as if it were not externally visible), the value of the property is a function, the new A () will call this construct function of a. So what does this construct function do?

1, create an object, let's say X.

2, if the A.prototype is an object (generally), then the a.prototype assigned to x.__proto__, otherwise (uncommon), please big Boss object, the Object.prototype assigned to x.__proto__.

3, call A.call (x), and the first parameter passes to the x we just created. This is settled, the function body of a is THIS.P = 1, this is the X. So x has this attribute of p, and X.P = 1.

4, in general, return X, when a is X. But there is also a special case , if the function of a is returned in the body of something, its type (typeof) is an object. So a is not pointing to X, but to what the a function returns.

Pseudo code is as follows:

  
   
   
var=new//is in fact not necessarily created with new, I'm not sure.
=
var= a.call (x)
If (typeof= ="object") {
return result ;
}
return x;

In our example, the A function returns undefined (because there is no return word), so a is x. But let's take an example to verify the special situation in the 4th step above:

    

Really

Constructor property of an object

Let's take a look at the first question left in the previous article.

  
   
   
function Base () {}
=1
var=new Base ();

function Derived () {}
= base;
var=new Derived ()

After executing the code above, Mybase.constructor is very easy to guess is base, then d.constructor? Is it derived?

     

No, also base, what's going on? Very simple, review the content of the next article will know: Because D itself does not have constructor attribute, so will go to d.__proto__ to find, d.__proto__ Is the derived.prototype, that is, base this object, base also did not constructor attributes, and then go up, to base.__proto__, that is, Base.prototype. It has a constructor attribute, which is the base itself. In fact, as far as I know, only the prototype of the constructor (function type Object) really own the object of the constructor property, and the constructor. Prototype.constructor = = constructor.

instanceof

So, what about instanceof?

    

As you can see from the diagram, D is an instance of base, derived, and object. Very reasonable, but how is this judged? Is this: for an expression of X instanceof constructor, if Constructor.prototype is in the prototype (__PROTO__) chain of X, return True. Obviously, the__proto__ chain of D is: Derived.prototype, Base.prototype, Object.prototype, there is no doubt that the results of the map. Therefore, instanceof is not related to the constructor attribute of the object.

Function and Object

Finally, answer the diagram at the beginning of the article.

Function and object are also object of function type, so it can be said that the function () constructs the thing (oneself constructs, I do not know specifically is this, but thinks so, quite reasonable. )

In other words, you can imagine the following code:

  
   
   
var=new Function ()
var=new

According to the law of the article, there will be function.__proto__ = = Function.prototype, as well as object.__proto__ = = Function.prototype, verify:

    

Function Instanceof object, which is obviously true, everything belongs to the object tube, and the __proto__ chain of the function points to: Function.prototype,object.prototype.

Object Instanceof function, because Function.prototype is in the __proto__ chain of object, so is true.

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.