Learn more about the basics of prototype and inheritance in JavaScript

Source: Internet
Author: User

Typically, an object in JavaScript is a pointer to prototype and a list of its own attributes. JavaScript uses the idea of write-time copying when creating objects.
Only the constructor has the prototype attribute, and the prototype chain inheritance is to create a new pointer to the constructor's prototype property.
The prototype attribute is special because it is determined by the traversal mechanism when the attribute is read in JavaScript. In essence it is a normal pointer.

Constructors include:
1.Object
2.Function
3.Array
4.Date
5.String

Let's give some examples here.

Copy Code code as follows:

<script>
Each function has a default property prototype, and this prototype constructor the default point to this function
Note that Person.constructor is not equal to Person.prototype.constructor. function instance with constructor property
function person (name) {
THIS.name = name;
};
Person.prototype.getName = function () {
return this.name;
};
var p = new Person ("Zhangsan");

Console.log (Person.prototype.constructor = = person); True
Console.log (P.constructor = = person); True, because p itself does not contain the constructor property, so this is actually called Person.prototype.constructor
</script>

Our aim is to show
1. Indicates that person inherits from animal
2. Indicates that P2 is an instance of person

Let's change the point of the prototype property so that person can get the method in the prototype attribute in animal. That person inherits from animal (man is Beast)

Copy Code code as follows:

<script>
function person (name) {
THIS.name = name;
};
Person.prototype.getName = function () {
return this.name;
};
var p1 = new Person ("Zhangsan");

Console.log (P.constructor = = person); True
Console.log (Person.prototype.constructor = = person); True

function Animal () {}

Person.prototype = new Animal ()//The reason for not using Person.prototype = Animal.prototype is because new has other functions, and finally summarizes.
var p2 = new Person ("Zhangsan");

(P2-> person.prototype-> animal.prototype, so p2.constructor is actually Animal.prototype.constructor)

Console.log (P2.constructor = = person); The output is false, but our intention is to be true here, indicating that P2 is an instance of person. At this point the purpose 1 was achieved, and the purpose 2 was not achieved.
</script>

But if we fix this,

Person.prototype = new Animal ();
Person.prototype.constructor = person;

P2.consturctor is right, pointing to person, indicating that P2 is an instance of the person class, but a new problem arises. At this point the purpose 2 was achieved, and the purpose 1 was not achieved.
Purpose 1 and Purpose 2 contradict each other at this time, because at this time prototype expressed the contradictions of the two meaning,
1 indicates who the parent class is
2 to reproduce as a prototype of your own instance

Therefore, we cannot use the prototype property directly to indicate who the parent is, but to use the getprototypeof () method to know who the parent class is.

Copy Code code as follows:

Person.prototype = new Animal ();

Person.prototype.constructor = person;

var p2 = new Person ("Zhangsan");

P2.constructor//Display function person () {}

Object.getprototypeof (Person.prototype). Constructor//Display function Animal () {}

We're going to separate the two concepts.


Finally, summarize:
When code var p = new Person () executes, new does the following things:

Create a Blank object

Create a pointer to Person.prototype

Pass this object through the This keyword into the constructor and execute the constructor.

If you use Person.prototype = Animal.prototype to indicate that person inherits from Animal, the Instanceof method also shows the instance of P and Animal, and returns True. This method is not used, This is because of the following two reasons:

1.new creates a new object, which avoids setting Person.prototype.constructor = person and also causes the value of Animal.prototype.constructor to become person. Instead, it dynamically gives the newly created object a constructor instance attribute, So the properties on the instance constructor Animal.prototype.constructor, so that Person.prototype.constructor and Animal.prototype.contructor are separated.

2.Animal the properties of this object cannot be passed to person

When you access an instance property by using the hasOwnProperty () method, it is clear when you access the prototype attribute.

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: [email protected] 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.