Talking about JS inheritance _ borrow constructor & combined inheritance, talking about js borrow Constructor

Source: Internet
Author: User

Talking about JS inheritance _ borrow constructor & combined inheritance, talking about js borrow Constructor

2. Borrow Constructors

To solve the problem of containing reference type values in the prototype, we use a technology called constructor stealing (also known as forged objects or classic inheritance ).

The basic idea of this technology is to call a super-Type constructor within a subclass constructor.

You can use the apply () and call () Methods to execute constructors on the newly created subclass object.

Function SuperType () {this. colors = ["red", "blue", "green"];} function SubType () {// inherits the SuperType. apply (this);} var instance1 = new SubType (); instance1.colors. push ("black"); alert (instance1.colors); // red, blue, green, blackvar instance2 = new SubType (); alert (instance2.colors); // red, blue, green

In the above example, the SuperType constructor is called in the environment of the newly created SubType instance (instance1 instance2. In this way, all object initialization code defined in the SuperType () function will be executed on the new SubType object. Therefore, each Subtype instance has its own copy of the colors attribute.

PASS Parameters

For the prototype chain, the borrow constructor has a great advantage, that is, the sub-Type constructor can want to pass Parameters in the super-Type constructor.

function SuperType(name){  this.name = name;}function SubType(){  SuperType.call(this, "Bob");  this.age = 18;}var instance1 = new SubType();alert(instance1.age);  //18alert(instance1.name);  //Bob

Questions about borrow constructors:

Methods are defined in constructors, so function reuse is impossible. In addition, the methods defined in the super-type prototype are invisible to sub-types.

3. Combined inheritance

Combination inheritance is also called pseudo-classical inheritance. It refers to the combination of prototype chains and technical groups that borrow constructors. This gives full play to the inheritance model of the two.

Use prototype chains to inherit prototype attributes and methods;

You can use constructors to inherit instance attributes.

In this way, the function is reused by defining the method on the prototype, and each instance has its own attributes.

Function SuperType (name) {this. name = name; this. colors = ["red", "blue", "green"];} SuperType. prototype. sayName = function () {alert (this. name);} function SubType (name, age) {// inherits the SuperType attribute. call (this, name); this. age = age;} // Inheritance Method SubType. prototype = new SuperType (); SubType. prototype. sayAge = function () {alert (this. age);} var instance1 = new SubType ("Bob", 22); instance1.colors. push ("black"); alert (instance1.colors); // red, blue, green, blackinstance1.sayName (); // Bobinstance1.sayAge (); // 22var instance2 = new SubType ("Alice", 21); alert (instance2.colors); // red, blue, greeninstance2.sayName (); // Aliceinstance2.sayAge (); // 21

In this example, the SuperType constructor defines two attributes: name and colors. The prototype of SuperType defines a method sayName ().

The SubType constructor imports the name parameter when calling the SuperType constructor and defines its own attribute age. Then assign the SuperType instance to the SubType prototype. The method sayAge () is defined in this prototype ().

In this way, two different SubType instances can have their own attributes, including the colors attributes, and the same method can be used.

The above discussion about the JS inheritance _ borrow constructors & combined inheritance is all the content shared by the editor. I hope you can give us a reference and support the house of helping customers.

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.