JS inheritance borrowing constructor inheritance and combinatorial inheritance

Source: Internet
Author: User

According to the less routine, more sincere this principle, continue to learn.


Borrowing constructor inheritance

In the process of solving the problem of containing reference type values in the prototype, the developer began using a constructor called borrowing
(constructor stealing) technology (sometimes called a forged object or classic inheritance). The basic idea of this technique is quite simple, namely
The superclass constructor is called inside the subtype constructor.

Basic mode

function supertype () {  this.colors = ["Red", "Blue", "green"];} Function subtype () {   //Inherits Supertype  Supertype.call (this);} var Instance1 = new subtype (); Instance1.colors.push ("Black"); alert (instance1.colors); "Red,blue,green,black" var instance2 = new subtype (); alert (instance2.colors); "Red,blue,green"

Basic ideas

The basic idea of borrowing a constructor is to use call or apply to copy (borrow) the property and method specified by this in the parent class to the instance created by the subclass. Because the This object is bound at run time based on the execution environment of the function. That is, in the global, this is equal to window, and when the function is called as a method of an object, this is equal to that object. The call, apply method can be used to invoke a method in place of another object. The call and apply methods can change the object context of a function from the initial context to the new object specified by Thisobj.

So, this borrowing constructor is the time of the new object (note that the new operator is different from the direct call, when it is called directly in the form of a function, this points to the instance created by Window,new), creates a new instance object, and executes the code inside the subtype, and subtype inside calls the Supertyep with the call, that is to change this point to a new instance, so the supertype inside of the relevant properties and methods are assigned to the new instance, Instead of assigning them to Suptype. All instances have properties and methods of these this that are defined by the parent class.

Advantage

With respect to the prototype chain, there is a great advantage to borrowing a constructor, that is, you can pass parameters to the superclass constructor in the subtype constructor. Because the property is bound to this, the invocation is assigned to the corresponding instance, and the values of each instance do not affect each other.

For example:

functionsupertype (name) { This. Name =name;}functionsubtype () {//inherits the Supertype, and also passes the parametersSupertype.call ( This, "Nicholas");//Instance Properties This. Age = 29;}varInstance =Newsubtype (); alert (instance.name);//"Nicholas";alert (instance.age);// in

Disadvantage

If you just borrow a constructor, you won't be able to avoid the problem with the constructor pattern--methods are defined in the constructor, so the reuse of the function is not possible. Also, methods defined in a super-type prototype are not visible to the subtypes, and all types can only use the constructor pattern. With these problems in mind, the technique of borrowing constructors is seldom used alone.

Combining inheritance

Combinatorial inheritance (combination inheritance), sometimes called pseudo-classical inheritance. is to combine the prototype chain and the technology that borrows the constructor into one piece, thus exerting a succession pattern of the two's length.

Basic ideas

The idea is to use the prototype chain to implement the inheritance of the prototype properties and methods, and to implement the inheritance of instance properties by borrowing constructors. This enables the reuse of functions both by defining methods on the prototype and ensuring that each instance has its own properties.

Basic model

function Supertype (name) {  this.name = name;  This.colors = ["Red", "Blue", "green"];} SuperType.prototype.sayName = function () {   alert (this.name);}; Function subtype (name, age) {//Inherit attribute  Supertype.call (this, name);  This.age = age;} Inheritance method Subtype.prototype = new Supertype (); SubType.prototype.constructor = subtype; SubType.prototype.sayAge = function () {alert (this.age);}; var Instance1 = new Subtype ("Nicholas"); Instance1.colors.push ("Black"); alert (instance1.colors); "Red,blue,green,black" Instance1.sayname (); "Nicholas"; Instance1.sayage (); 29var Instance2 = new Subtype ("Greg"); alert (instance2.colors); "Red,blue,green" Instance2.sayname (); "Greg"; Instance2.sayage (); 27

Advantage

Combined inheritance avoids the defects of prototype chains and borrowed constructors, and blends their advantages into the most commonly used inheritance patterns in JavaScript.

Disadvantage

The biggest problem with combinatorial inheritance is that, in any case, a two-time super-type constructor is called: one time when a subtype prototype is created, and the other is inside the subtype constructor. Although the subtype eventually contains all of the instance properties of the superclass object, we have to override these properties when the subtype constructor is called.

  

To be Continued ...

JS inheritance borrowing constructor inheritance and combinatorial inheritance

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.