New in JavaScript

Source: Internet
Author: User

What does new do?

First, let's try to see what the new example contains.

function Base () {     this. str= "Tong";}  var a=New  base (); Console.dir (a);

The results in the controller are as follows:

We see that it contains str at the same time, also contains the _proto_ object, the Internet to check, this _proto_ is actually the constructor of the prototype constructor.

Then, let's take a look at what's inside _proto_.

As we can see, there is also a _proto object, which is consistent with the _proto_ contained in the above example .

Let's focus on the constructor object, which refers to the function base (), which is our constructor, but this is actually inaccurate, let's experiment.

        function Base () {            this. str= "Tong";        }          function Other () {};         var New Base ();        Console.dir (b);

Look at the results:

Constrctor became other, so it was wrong to say that we had a previous understanding of constructor. When a function is created, the corresponding prototype is automatically generated, which contains an constructor attribute, using the new constructed instance, which can be found through the prototype chain.constructor。

Finally, let's experiment with the return value in the constructor. We don't do that very often, but we also need to look at the results to further analyze new.

       function Base () {            this. str= "Tong";              return 1;        }         var a=New  Base ();        Console.dir (a);

The result is:

Just like the original.

Let's go back to the object and try it:

       function Base () {            this. str= "Tong";              return [1];        }

Results:

The result is different, as you can see from the example above, if the object is returned, the constructed instance is overwritten.

Let's conclude that new has done at least 4 things:

//new Base ();//1. Create an empty object objvarobj = {}; //2. Set the __proto__ of obj as the prototypeobj.__proto__ =Base.prototype; //3. Calling the base function using obj as the contextvarRET =Base.call (obj); //4. If the constructor returns the original value, the return value is ignored and if an object is returned, the constructed instance is overwrittenif(typeofRET = = ' object '){      returnret;} Else {    returnobj;}

Ii. Deficiencies of new

In the language of Javascript (javascript:the good Parts), Douglas believes that the use of keywords should be avoided new :

If you forget to include the new prefix when calling a constructor function and then this is not being bound to the new object . Sadly, this is bound to the global object, so instead of augmenting your new object, you'll be clobbering global VA Riables. That's really bad. There is no compile warning, and there is no runtime warning.

The main idea is that new if you forget the keyword when you should use it new , some problems will arise. The most important problem is the impact of the prototype lookup, the prototype lookup is __proto__ carried out, and any function is an Function instance, once useless new , you will find what properties are not found, because the path of the search is broken.

New in JavaScript

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.