Can't sleep at night, second essay, talk about JS's creation instantiation process

Source: Internet
Author: User

Daughter-in-law day overtime too tired, I, sleep too much during the day, too excited at night, I secretly took a notebook to the living room to write blog ~

The previous article may have seen a lot of people think it is a joke, that really is a joke, but in the actual development process, many young coder for writing do not write a semicolon very much, to know that the real production environment code is far more complex than I pest to the code, so it is likely not to mislead me , you can not see, so wasted an afternoon of valuable development time, so write code or to standardize some.

The second article I still do not want to talk too much in-depth technology, or say two "lace news", consolation just, crossing have the interest you look, no interest can also spray.

is still a subject of this kind:

1 var function (p) {2     this. p = p; 3 }; 4 var New Model (1); 5 6 Console.log (model.constructor); 7 Console.log (Model); 8 console.log (Model = = = Model.constructor);

Very simple an instantiation process, because there is no mood, so directly do not suspense, directly to tell the point, to console.log out

Here I do not ask myself to answer, directly to say, the printing result is:

function model (p)function Model (p) true

Here may know people understand what I want to say, do not know people still do not understand what I want to say, seems to be a nonsense = =+

As you can see, the printed model.constructor is the model.

Right, as with all programming, when new is an object, it automatically executes the object's constructor, which is the model here.

Then we do a seemingly only JS to become a language to do things, and dynamically modify the model:

1 varModel =function(p) {2      This. P =p;3 };4 varModel =NewModel (1);5 6 Console.log (model.constructor);7 Console.log (Model);8Console.log (Model = = =model.constructor);9 TenModel =function(x) { OneConsole.log (1); A } -  - Console.log (model.constructor); the Console.log (Model); -Console.log (Model = = = Model.constructor);

The printing results are as follows:

function Model (P) function Model (P) true function Model (P) function Model (x) false

From this code we can tell that when the model is modified dynamically it does not affect the constructor of the model object that has already been instantiated.

That is, once the model is instantiated, the constructor for that instance is deterministic, and the instance and model have no relation to the half-dime.

The next thing I'm going to talk about is some jumping, trying to do it yourself:

1 varModel =function(p) {2      This. P =p;3 };4 varModel =NewObject ();5model.__proto__ =Model.prototype;6 Console.log (model.constructor);7 varModel2 =NewModel (1);8Console.log (Model2.constructor);
9Console.log (Model.constructor = = = Model2.constructor);

Printing results We can see that the constructor of the two is a hair-like

function Model (P) function Model (P) true

Then we do the following:

1 varModel =function(p) {2      This. P =p;3 };4 varModel =NewObject ();5model.__proto__ =Model.prototype;6 Console.log (model.constructor);7 varModel2 =NewModel (1);8 Console.log (model2.constructor);9Console.log (Model.constructor = = =model2.constructor);Ten  OneModel.call (model,1); A Console.log (model); - Console.log (MODEL2); -Console.log (Model.constructor = = = Model2.constructor);

The printing results are as follows:

function Model (P) function Model (P) true  11}true

That is, in this case, except one is printed out is the object one is printed out is the model, the other two instances without any difference, the same constructor, the same number of members and names

Let me briefly summarize what I said this time:

Instantiating an object is essentially instantiating an object model, then pointing the object's prototype to the model's prototype, and then invoking the model's constructor with that object to initialize all the members of the object, at which point the constructor of the object has no relation to the model. Even if the model is modified dynamically, it will not have any effect on the model.

Niche Caishuxueqian, if wrong, also look correct!

Today is here, it is estimated that no one to see, if someone to see, I will prepare some interesting things.

Can't sleep at night, second essay, talk about JS's creation instantiation process

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.