In JavaScript, inheritance is a wonderful way to implement interface inheritance, and can only rely on prototype inheritance.
Prototype chain
a prototype is an object, and an instance created through a constructor has a pointer to the prototype's properties and methods. In this way, the instance object takes the property method of the constructor and the property method of the stereotype, and then points to the instance of the constructor that needs to be inherited, so that all the property methods of the instance can be inherited.
Look at the following demo code:
Declares the superclass, adding the properties and method
function super () {This.property = True by constructors and prototypes
;
}
Super.prototype.getSuperValue = function () {return
this.property;
};
Declares a constructor function for a subclass
subtype () {
this.subproperty = false;
}
The prototype of the subclass is directed to an instance of the superclass to get all the
Subtype.prototype = new super ();
SubType.prototype.constructor = subtype;
SubType.prototype.getSubValue = function () {return
this.subproperty;
};
Subclass to create an object, test whether to inherit superclass methods and properties
var instance = new Subtype ();
Console.log (Instance.getsupervalue ());
The default prototype for all functions is an instance of Object, so the default prototype contains an internal pointer to the Object.prototype.
Use instanceof and isprototypeof to determine the relationship between a prototype and an instance:
Instance instanceof Object;
Object.prototype.isPrototypeOf (instance);
When using a prototype chain, you need to define the method carefully. Subclasses need to override a method or extension of a superclass, and be sure to put it behind the statement that replaces the prototype so that it takes effect. In addition, when you implement inheritance through a prototype chain, you cannot use object literals to create a prototype method, which overrides the prototype chain:
......
Subtype.prototype = new Super ();
Subtype.prototype = {
...
};
This overrides the prototype chain by replacing the pointer to a new object.
The inheritance method of prototype chain is flawed, there are two main problems:
1, the prototype from the containing reference type value is shared by all instances.
The previous article describes that a prototype property containing a reference type value is shared by all instances, one instance modifies, and the other instances change, so you need to define the attribute in the constructor. When the prototype chain inherits, regardless of whether the attribute in the superclass is defined in the constructor or the prototype, it all becomes the instance object quilt class inheritance, thus having an effect on the instance of the subclass.
2, when you create an instance of a subtype, you cannot pass parameters to the superclass constructor.
The inheritance of the prototype chain directly points the subclass prototype to an instance of the superclass, at which point parameters can be passed to the superclass. However, when a subclass creates an instance, it can only pass arguments to the constructor of the subclass, not to the constructor of the superclass.
Therefore, in practical applications, the prototype chain is rarely used alone.
Some code practices related to
Identify a stereotype attribute
function Hasprototypeproperty (object, name) {return
name in Object &&!object.hasownproperty (name);
}
Using a prototype object in a constructor
function person (name) {
this.name = name;
}
Person.prototype = {
Constructor:person,
sayname:function () {
console.log (this.name);
},
Tostring:function () {
}
};
var person1 = new Person (' Nicholas ');
var person2 = new Person (' Greg ');
Console.log (person1 instanceof person); True
console.log (person1.constructor = = person);//True
console.log (person1.constructor = = Object);// False
Console.log (person2 instanceof person);//True
console.log (person2.constructor = = person);//true< C23/>console.log (Person2.constructor = = = Object); False
Object inheritance
var person1 = {
name: ' Nicholas ',
sayname:function () {
console.log (this.name);
}
;
var person2 = object.create (Person1, {
name: {
configurable:true,
enumerable:true,
value: ' Greg ', C11/>writable:true
}
});
Person1.sayname (); Nicholas
person2.sayname ();//Greg
Console.log (person1.hasownproperty (' sayname '));//True
Console.log (person1.ispropertyof (Person2)); True
Console.log (Person2.hasownproperty (' sayname '));//False
Module mode
var person = (function () {
var age = =;
function Getage () {return age
;
}
function Growolder () {
age++;
}
return {
name: ' Nicholas ',
getage:getage,
growolder:growolder
};
} ());
The constructor of the scope
function person (name) {this.name = name;}
Person.prototype.sayName = function () {console.log (this.name);};
var person1 = person (' Nicholas '); Console.log (person1 instanceof person); False Console.log (typeof Person1); Undefined Console.log (name); Nicholas