[Path to a javascript guru 04] understanding prototype

Source: Internet
Author: User
Tags hasownproperty
Preface

At noon, I went to the drugstore and called my weight. Good guy! I have succeeded in losing weight, and I am 10 kg thinner than last month! So if you want to lose weight, go to Zhengzhou...

Then I went back to sleep for a while and remembered the interview on Wednesday. It was a little pity and a little touched.

After returning to Chengdu this time, I have not yet formally looked for a job. I am also reviewing myself. I think of a question the interviewer asked at the last time:

Do you have something to be proud? Do you have anything to prove your character?

This is actually something I want to boast about myself, but after I come out to work, there are a few brilliant things, but it doesn't matter if I can't say it in front of him, as a result, I insisted on writing my blog for three months.

But in his opinion, 3 months does not seem to matter... In fact, it's really nothing to think about for three months. I still have a lot of things I don't understand, in various aspects.

So next week, I should settle down with a job, make sure that I have one or two products, and stick to writing this year's blog first! I hope that I can be proud to say that I have been studying for 2 or 3 hours every day and shared my blog.

PS:In fact, when I am sleeping, I mainly think about how my ticket has not been reimbursed... No money on the account... :)

Now, let's go to our afternoon study. Let's take a look at our prototype this time. I came back to the object-oriented interface because we wrote code before this thing, here we will focus on it directly.

Prototype chain
Each function we create contains a prototype attribute. It is a pointer pointing to an object that contains attributes and methods that can be shared by all instances of a specific type. Popular: Prototype is a template, and a new template is a copy of it.

Each prototype object contains a constructor attribute, which has little significance and is often overwritten. However, it points to the constructor and we may use it in the type judgment area.

After a new instance is created, the instance contains an internal attribute (pointer ,__ PROTO _) of [[prototype], pointing to the prototype of the constructor, you cannot solve this problem. We have several methods to remember:

We use isprototypeof to determine whether an object is my prototype.
Hasownprototype can be used to check whether an attribute exists in an instance or whether the prototype is a prototype. If this attribute is not a prototype property, true is returned.

After talking about this, let's go to the previous figure:

1 var person = function (name, age) {2 This. name = Name; 3 this. age = age; 4}; 5 person. prototype. getname = function () {6 return this. name; 7}; 8 var y = new person ('Ye xiaohuan ', 30 );

We generally do this, but name and age can be accessed outside.

1 var s1 = Person.prototype.isPrototypeOf(y); //true;2 var s2 = y.hasOwnProperty('name'); //true3 var s3 = y.hasOwnProperty('id'); //false4 var s4 = y.hasOwnProperty('getName');//false
Relationship between prototype chains during inheritance

Here we are direct, because our person function inherits from the object constructor. To put it bluntly, the person prototype is an instance of the object constructor:

Since I have written similar articles before, I will not talk about them here.

Do the first question for two questions
1 var proto = {A: 1}; 2 function Cls () {}; 3 var obj1 = new Cls (); 4 Cls. prototype = proto; 5 // What is obj1.a now?

Here CLS is used as the constructor. After obj1 is instantiated, its internal attribute [[prototype] points to the CLS prototype object (which is equivalent to a copy), and the prototype of the constructor points

This question is actually seduce us, and we are likely to answer 1. In fact, the answer is undefined. After the constructor CLS is created, its prototype object has been formed, and the link between CLs and its prototype is cut off,

The _ PROTO _ ([prototype]) of obj1 still points to the original prototype instead of Proto.

Question 2
1 function Cls () {}; 2 var obj1 = new Cls (); 3 CLs. Prototype. A = 1; 4 // What is obj1.a now? 5 alert (obj1.a );

This question is different from the previous one. Although the prototype is changed afterwards, it is the same object on the changed stack. Therefore, the answer is 1.

Question 3

I found a closure question. Take it out by the way.

1 function build () {2 var I; 3 return function () {alert (I ++);} 4} 5 var F1 = build (); 6 var F2 = build (); 7 var S1 = F1 = F2; 8 var S = ''; 9 // check whether F1 and F2 are the same function, what are the effects of calling F1 () F2.

This question is a bit interesting. To make it run normally, let's give I a 1.

If the return value of the function is removed here, F1 and F2 are the same. However, because the closure is generated in F1 and F2, both F1 and F2 maintain their own environments and closures.

Let's take a look at this question:

During Row 5 execution, an execution environment and related scope chains will be created, arguments and this will be initialized, and the active object will be initialized together with I and anonymous functions (Raise any questions)

After lines 5 are completed, the execution environment and scope chain are recycled, but the activity object is left.

When F2 is initialized in Row 6, this process is re-executed.

So the activity objects used during the last call are inconsistent, so the two functions are not one function.

Error: I thought about it when I was eating. The activity objects here should not include this

PS: I cannot find a good question for the moment. If you have a good question, please stay

Conclusion

I made a review today. I want to learn more about it next time. Now I want to have a meal first.

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.