JavaScript Tour-11th station: Prototypes are not good to understand?

Source: Internet
Author: User

Original: JavaScript Tour-11th station: Prototypes are not good to understand?

Write this, my JS series is nearing the end, so this series will not leave JS to achieve the core of object-oriented-prototype, some people say the prototype is not good understanding, in fact, want to system

Understanding of the prototype, the most convenient way is to look at the classic book, less read some blog, blog This thing is only Bo Master's own personal understanding, at best, some flavor of the seasoning.

 

One: Inheritance

If you are familiar with C #, you will know that all classes inherit from object, so I have the functionality of object, such as the person class I define in.

As you can see, inheritance is everywhere in C #, and the next thing I want to do is customize the inheritance, such as the student class I defined in, let it inherit the Person.name property.

These for the people who play C # are very Sitong, then the next question, the real object-oriented things, in JS how to play it? Of course, we'll use the famous

The prototype property.

Second: Using JS to imitate C # inheritance

1. Inherit object by default

We all know that all the reference types in JS also inherit from object, so it has the function of object, but have you ever considered, for example, how does the person in the person inherit

All the properties and methods of object?

When you see it, are you curious? In fact, the principle is really simple, with chorme watch expressions you will be clear.

At first sight don't know if you will dizzying? Listen to me slowly explain, from the above diagram is not difficult to see, in fact, there is a relationship between the prototype chain:

p.__proto__ =person.prototype

person.prototype.__proto__-New Object ()

I don't know, do you read? In fact, the most important thing here is the __proto__ property, first you have to know, each instance has this __proto__ attribute, because this is the core, for example, you want to find

P.tostring () method, the JS engine will take precedence in the person function to find the toString () method, found that there is no ... Flower rub ... You can only continue up through the P.__proto__ property

Find, to Person.prototype, you can see that prototype is an object with a constructor attribute, because there is only one property, so the Tostirng () method is not found,

Then we found the object along the person.prototype._proto__, where we found the ToString () method.

2. Custom Inheritance

We know that prototype is a very important attribute, in order to imitate the C # Student class inherits from the person class, this time I need to do is let studnet.prototype=new person () just fine.

You can see that the student instance already contains the name attribute, and we now know that there is a prototype chain lookup process, such as I now find the new person by student.__proto__ (),

And then you see that new person () has the name attribute, and I think you now know that there is also a __proto__ property in the person function, which is pointing to object, if I am in New person ()

is not found, it will continue to look up through person.__proto__ (Student.prototype.proto__), until the top has been found.

Three: Detailed prototype

1. What exactly is prototype?

From the previous chapter I think you should have a macro understanding of prototype, you can see in fact prototype is just a constructor property of the object, where the constructor attribute refers to the

To a pointer to the current function, the code is restored as follows:

1<script type= "Text/javascript" >2         functionPerson () {3              This. Name = "Ctrip";4         }5 6Person.prototype = {7Constructor:person//constructor pointing to person8         };9 Ten         varp =NewPerson (); One</script>

2. Prototype the above properties can be shared by all instances.

This can be shared because each instance has a __proto__ attribute, including the prototype property of the function, which also has a __proto__ attribute, because prototype essentially

is also an instance of an object, so JS keeps track of the __proto__ property to object when it finds out if a property exists.

3. What if the properties in function conflict with the prototype attribute?

See the answer, I think you are very clear, after all, you have understood the prototype chain lookup, because the JS engine lookup process is first from this function lookup, if found on the return, can not find to continue through

__proto__ look up, very well understood.

JavaScript Tour-11th station: Prototypes are not good to understand?

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.