The Inheritance in JS

Source: Internet
Author: User

That's the inheritance, that's how I see it. This inheritance is generally for the parent and child methods, that is to say there are two methods, a parent method, a child method. The child method can have all the properties and functions of the parent method, and for us, there are 3 common inheritance in JS.

First prototype chain inheritance: Sub-object He has a default property called __proto__, which is pointing to the method itself, and the method itself is not with the parent object's method or property, so he has to call his prototype property, this prototype attribute he points to an object, The object is the prototype of the sub-method, then the prototype is instantiated with the parent method, so he is also the __proto__ of his parent object, this __proto__ again points to the parent object itself prototype, this prototype also called him inside of the properties and methods, In this case, his prototype chain is inherited.

For example:

function Parent () {

THIS.name = ' Mike ';

}

function Child () {

This.age = 12;

}

Child.prototype = new parent ();//child inherits the parent, through the prototype, forms the chain

Obj.prototype = = object. __proto__
var p = new per ();
Alert (P.__proto__==per.prototype)//true
per.prototype.__proto__


The second is called a constructor inheritance, and the most important thing about constructor inheritance is impersonating, the parent object defined in the child object
Inside, you can refer to the parent object's properties and methods within the child object, constructor inheritance I feel relatively simple.

Gca

function Myfun () {
This.name=
This.age=
This.show = function () {}

}

var obj1 = new Myfun ();
var obj2 new Myfun ();


Then there is the hybrid inheritance, that is, the structure, and the prototype, such a way of inheritance.

For example:

function Myfun () {
THIS.name
This.show=function () {alert (123)}
}
Myfun.prototype.age=12
MyFun.prototype.info = function () {alert (456)}
myfun.prototype={
Name: "Zhangsan",
Age:18,
Show:function () {}
}


This is my view of succession.
(The difference between call and apply: Although both are inherited properties, there are different writing formats
Here the call method is followed by a string series after this
The Apply method is also passed by this but the property is made up of arrays

Call (This,name,age,str,demo) string sequence
Apply (This,[name,age]) array

---restore content ends---

---restore content starts---

That's the inheritance, that's how I see it. This inheritance is generally for the parent and child methods, that is to say there are two methods, a parent method, a child method. The child method can have all the properties and functions of the parent method, and for us, there are 3 common inheritance in JS.

First prototype chain inheritance: Sub-object He has a default property called __proto__, which is pointing to the method itself, and the method itself is not with the parent object's method or property, so he has to call his prototype property, this prototype attribute he points to an object, The object is the prototype of the sub-method, then the prototype is instantiated with the parent method, so he is also the __proto__ of his parent object, this __proto__ again points to the parent object itself prototype, this prototype also called him inside of the properties and methods, In this case, his prototype chain is inherited.

For example:

function Parent () {

THIS.name = ' Mike ';

}

function Child () {

This.age = 12;

}

Child.prototype = new parent ();//child inherits the parent, through the prototype, forms the chain

Obj.prototype = = object. __proto__
var p = new per ();
Alert (P.__proto__==per.prototype)//true
per.prototype.__proto__


The second is called a constructor inheritance, and the most important thing about constructor inheritance is impersonating, the parent object defined in the child object
Inside, you can refer to the parent object's properties and methods within the child object, constructor inheritance I feel relatively simple.

Gca

function Myfun () {
This.name=
This.age=
This.show = function () {}

}

var obj1 = new Myfun ();
var obj2 new Myfun ();


Then there is the hybrid inheritance, that is, the structure, and the prototype, such a way of inheritance.

For example:

function Myfun () {
THIS.name
This.show=function () {alert (123)}
}
Myfun.prototype.age=12
MyFun.prototype.info = function () {alert (456)}
myfun.prototype={
Name: "Zhangsan",
Age:18,
Show:function () {}
}


This is my view of succession.
(The difference between call and apply: Although both are inherited properties, there are different writing formats
Here the call method is followed by a string series after this
The Apply method is also passed by this but the property is made up of arrays

Call (This,name,age,str,demo) string sequence
Apply (This,[name,age]) array

The Inheritance in JS

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.