“This is the 14th day of my participation in the November Gwen Challenge.The final text challenge in 2021” |
---|
Inherited parent class (inherited from whom, providing inherited properties)
/ / parent -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
function Person(name){ // Add arguments to the constructor
this.name=name
this.sayName =function(){
alert(this.name)
}
}
Person.prototype.age = 18; // Add stereotype attributes to the constructor
Copy the code
4. Original type inheritance
Focus on
Wrap an object with a function and return a call to the function. The function becomes an instance or object that you can add attributes to at will. Object.create () works this way.The characteristics of
: is similar to copying an object, wrapped in a function.disadvantages
:- All instances inherit properties from the stereotype.
- Reuse cannot be achieved. (New instance attributes are added later)
// The original type inherits ---------------------------
// Encapsulate a function container that outputs the object and holds the inherited prototype
function content(obj){
function F(){}
F.prototype=obj; // Inherits the parameters passed in
return new F() // Return the function object
}
var sup = new Person(); // Get an instance of the parent class
var sup1 = content(sup)
console.log(sup1.age) //18 inherits the parent function attributes
Copy the code
Parasitic inheritance
Focus on
: Is to give the original type inheritance outside the shell.advantages
: No custom type is created, since the shell returns the object (this), the function becomes the new object created.disadvantages
: No prototype, can’t reuse.
// Parasitic inheritance ----------------------
// Pass parameters to the shell of the original type inheritance
function subObject(obj){
var sub = content(obj)
obj.name= 'pan';
return sub
}
// Add attributes to the object
var sup2 = subObject(sup)
console.log(typeof subObject) //function
console.log(typeof sup2) //object
console.log(sup2.name) //pan
Copy the code
- Parasitic combinatorial inheritance (common)
parasitic
: Returns an object within a function and then callscombination
The prototype of a function is equal to another instance. 2. Use apply or call to introduce another constructor that takes argumentsFocus on
: Fixed combinatorial inheritance issues
// Parasitic combinatorial inheritance ---------------------
/ / the parasitic
function content(obj){
function F(){}
F.prototype=obj; // Inherits the parameters passed in
return new F() // Return the function object
}
// Content is just another expression for F instances
var con = content(Person.prototype)
// The prototype chain of the con instance (F instance) inherits the prototype of the superclass function
// The above is more like prototype chain inheritance, but inherits the stereotype attributes
/ /
function Sub(){
Person.call(this) // Inherits the parent constructor attribute
} // Address the weakness of the combined-two-call constructor property
/ / the key
Sub.prototype = con; // Inherit the con instance
con.constructor = Sub; // Be sure to fix the instance
var sub1 = new Sub();
// The Sub instance inherits the constructor attribute, the superclass instance, and the function attribute of con
console.log(sub1.age)
Copy the code
Implementation of JavaScript Inheritance -1
Several implementations of JavaScript inheritance -2