Javascript prototype object popular "singing" _ javascript skills-JS tutorial

Source: Internet
Author: User
I was dazzled by the book's statements about the prototype object. I did not know what it was talking about. I checked a lot of information and finally got some understanding, below I will talk about my understanding of prototype objects in plain words. 1. Important knowledge points about prototype objects

First, you must know a very important knowledge point. In a word, all objects have original objects.

2. Comparison of understanding in other languages

The prototype object is the static attributes and static methods of classes in other languages. It is always static-static. The principle is that there is only one copy in the memory.

3. Image in memory:

First, before generating js objects, we need to create a constructor (this is unknown, so don't look down), as shown below:

The Code is as follows:


Function Person (name _, age _){
This. name = name _;
This. age = age _;
}



Next, we need a new object. Here, we have three new (Person) objects: "Zhang San" "Li Lei" "Han Meimei". They come from the same constructor Person:

This is the case in the memory. Each object has its own name and age memory. Here, how many new objects will be created.

It is better to understand this. Next we will add an attribute. location, as shown below:

The Code is as follows:


Function Person (name _, age _){
This. name = name _;
This. age = age _;
This. location = "Earth ";
}


In this case, the memory of the three new users is as follows:

Here we can see that all three objects have a "Earth" memory space. here you have to move your brain. All three of them have the Earth's memory. Can we do this?

Do you think so? In this way, we only need one earth. You can see this. Well, if the space in the public space is an object, it is a so-called prototype object. Ele. Me? That's it?

Yes, that's it.

The most important role of a prototype object is to separate constants and methods into itself and provide them to other "own objects". Finally:

4. Introduce the prototype object at the code level.

It is an object in the memory. Now we are operating from the code.

The Code is as follows:


Function Person (name _, age _){
This. name = name _;
This. age = age _;
This. location = "Earth ";
}
// Three specific objects
Var zhangsan = new Person ("zhangsan", 21 );
Var lilei = new Person ("lilei", 21 );
Var hanmeimei = new Person ("hanmeimei", 21 );
// Their prototype object is
Person. prototype. location = "Earth ";
Person. prototype. killPerson = function (){
Return "murder ";
};


Here is a question: We know the prototype object, but how can we access the properties in the prototype object? How can we get the location and use the killPerson method?
See:

The Code is as follows:


Alert (zhangsan. location );
Alert (zhangsan. killPerson ());


In this way, you can access the object, but the premise is that there is no definition of location and killPerson in your object attributes. Otherwise, the original object will be overwritten. This involves the prototype problem, that is,
In zhangsan. when location is used, we first check the zhangsan object itself. We know that there are name, age, and prototype pointer attributes in figure 3. there is no location. If no location is found, it will continue to search for the original object to see if the location attribute can be found. If yes, it will call the attribute of the original object.
So

The Code is as follows:


Alert (zhangsan. location) will output "Earth"
Alert (zhangsan. killPerson () will output "Killing"


The above is my personal understanding of the prototype object and I hope to help you.

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.