JavaScript Advanced Programming (4th Edition) Reading Sharing Note Recording
Applicable to comrades who are just getting started
prototype chain
The meaning of the prototype chain: each constructor has a prototype object (prototype), the prototype object has a property ( constructor ) pointing back to the constructor, and the instance has an internal pointer (__proto__) pointing to the prototype object . What if the prototype is an instance of another type (we make the prototype object equal to an instance of another type)? That means that the prototype itself has an internal pointer to another prototype, which in turn has a pointer to another constructor. This constructs a prototype chain between the instance and the prototype. This is the basic idea of the prototype chain.
Prototype objects, constructors, and instance explanations are mentioned in the previous chapter
example:
function SuperType() {
this.property = true;
}
SuperType.prototype.getSuperValue = function() {
return this.property;
};
function SubType() {
this.subproperty = false;
}
// 继承 SuperType
SubType.prototype = new SuperType();
SubType.prototype.getSubValue = function () {
return this.subproperty;
};
let instance = new SubType();
Such a layer-by-layer progression constitutes a chain of instances and prototypes, forming a prototype chain
default prototype
Prototype and Inheritance
console.log(instance instanceof Object); // true
console.log(instance instanceof SuperType); // true
console.log(instance instanceof SubType); // true
console.log(Object.prototype.isPrototypeOf(instance)); // true
console.log(SuperType.prototype.isPrototypeOf(instance)); // true
console.log(SubType.prototype.isPrototypeOf(instance)); // true
Prototype chain problem
function SuperType() {
this.colors = ["red", "blue", "green"];
}
function SubType() {}
// 继承 SuperType
SubType.prototype = new SuperType();
let instance1 = new SubType();
instance1.colors.push("black");
console.log(instance1.colors); // "red,blue,green,black"
let instance2 = new SubType();
console.log(instance2.colors); // "red,blue,green,black"
Stealing constructor
In order to solve the inheritance problem caused by the prototype containing the reference value
The basic idea is simple: call the parent class constructor in the subclass constructor. Constructors can be executed in the context of the newly created object using the apply() and call() methods.
example:
function SuperType() {
this.colors = ["red", "blue", "green"];
}
function SubType() {
// 继承 SuperType
SuperType.call(this);
}
let instance1 = new SubType();
instance1.colors.push("black");
console.log(instance1.colors); // "red,blue,green,black"
let instance2 = new SubType();
console.log(instance2.colors); // "red,blue,green"
Composition inheritance
Combination inheritance makes up for the shortcomings of prototype chain and stolen constructor, and is the most used inheritance mode in JavaScript
function SuperType(name){
//实例属性
this.name = name;
this.colors = ["red", "blue", "green"];
}
//原型链属性,方法
SuperType.prototype.sayName = function() {
console.log(this.name);
};
function SubType(name, age){
// 继承属性
SuperType.call(this, name);
this.age = age;
}
// 继承方法
SubType.prototype = new SuperType();
SubType.prototype.sayAge = function() {
console.log(this.age);
};
let instance1 = new SubType("Nicholas", 29);
instance1.colors.push("black");
console.log(instance1.colors); // "red,blue,green,black"
instance1.sayName(); // "Nicholas";
instance1.sayAge(); // 29
let instance2 = new SubType("Greg", 27);
console.log(instance2.colors); // "red,blue,green"
instance2.sayName(); // "Greg";
instance2.sayAge(); // 27
prototypal inheritance
Example of prototypal inheritance:
let person = {
name: "Nicholas",
friends: ["Shelby", "Court", "Van"]
};
let anotherPerson = object(person);
anotherPerson.name = "Greg";
anotherPerson.friends.push("Rob");
let yetAnotherPerson = object(person);
yetAnotherPerson.name = "Linda";
yetAnotherPerson.friends.push("Barbie");
console.log(person.friends); // "Shelby,Court,Van,Rob,Barbie"
The object() method used here creates a temporary constructor, assigns the incoming object to the prototype of the constructor, and then returns an instance of the temporary type.
parasitic compositional inheritance
function inheritPrototype(subType, superType) {
let prototype = object(superType.prototype); // 创建对象
prototype.constructor = subType; // 增强对象
subType.prototype = prototype; // 赋值对象
}
function SuperType(name) {
this.name = name;
this.colors = ["red", "blue", "green"];
}
SuperType.prototype.sayName = function() {
console.log(this.name);
};
function SubType(name, age) {
SuperType.call(this, name);
this.age = age;
}
inheritPrototype(SubType, SuperType);
SubType.prototype.sayAge = function() {
console.log(this.age);
};