原型链与继承
- 0
#讨论区
00条评论
实时对话
loading...
每个对象创建时从另一个对象上继承属性,另一个对象就是原型(原型对象)
js
__proto__
和constructor
属性是对象所独有的__proto__
和constructor
实例对象和原型对象通过__proto__
层层关联,直到null为止,形成原型链
instanceof
检测是否在原型链上
javascript
instanceof
javascript
js
js
缺点
js
缺点
js
缺点
多次实例化
js
缺点
js
最优的继承方式
js
function Test() {}
const test = new Test()
//1
console.log(Object.getPrototypeOf(test));
//2
console.log(test.__proto__);
//3
console.log(Test.prototype);
//4
console.log(test.constructor.__proto__);
//5
console.log(Reflect.getPrototypeOf(test))
//6
console.log(Test.prototype === test.__proto__)//true
function Test() {
}
const test = new Test()
console.log(test instanceof Test)//true
console.log(test instanceof Object)//true
console.log(Test instanceof Function)//true
console.log(Test instanceof Object)//true
function MyInstanceof(objInstance, Func) {
let objProto = objInstance.__proto__
let FuncP = Func.prototype
while (true) {
if (!objProto) return false
if (objProto === FuncP) return true
objProto = objProto.__proto__
}
}
// ES6的继承
class Parent {
constructor() {
this.name = "大人"
this.hairColor = "黑色"
}
}
class Child extends Parent {
constructor() {
super() //调用父级的方法和属性
this.name = "小孩"
}
}
let c = new Child()
console.log(c.name, c.hairColor) //小孩 黑色
let p = new Parent()
console.log(p.name, p.hairColor) //大人 黑色
// 定义父类
function Parent() {
this.name = 'Jack';
}
// 父类原型添加方法
Parent.prototype.getName = function () {
return this.name;
};
// 子类
function Child() {
}
// 子类的原型设置为父类Parent的实例
Child.prototype = new Parent();
Child.prototype.constructor = Child
// 实例化子类
const child = new Child();
console.log(child.getName()); // Jack
function SuperType() {
this.colors = ['red', 'blue', 'green'];
}
function SubType() {
// 继承 SuperType
SuperType.call(this);
}
const instance1 = new SubType();
instance1.colors.push('black');
console.log(instance1.colors); // [ 'red', 'blue', 'green', 'black' ]
const instance2 = new SubType();
console.log(instance2.colors); // [ 'red', 'blue', 'green' ]
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.constructor = SubType;
SubType.prototype.sayAge = function () {
console.log(this.age);
};
const usr1 = new SubType('Nicholas', 29);
usr1.colors.push('black');
console.log(usr1.colors); // [ 'red', 'blue', 'green', 'black' ]
usr1.sayName(); // Nicholas
usr1.sayAge(); // 29
const usr2 = new SubType('Greg', 27);
console.log(usr2.colors); // [ 'red', 'blue', 'green' ]
usr2.sayName(); // Greg
usr2.sayAge(); // 27
const person = {
name: 'Nicholas',
friends: ['Shelby', 'Court', 'Van'],
};
const anotherPerson = Object.create(person);
anotherPerson.name = 'Greg';
anotherPerson.friends.push('Rob');
const anotherPerson2 = Object.create(person);
anotherPerson2.name = 'Linda';
anotherPerson2.friends.push('Barbie');
console.log(person.friends); // [ 'Shelby', 'Court', 'Van', 'Rob', 'Barbie' ]
function createAnother(original) {
const clone = Object.create(original); // 通过调用函数创建一个新对象
clone.sayHi = function () {
// 以某种方式增强这个对象
console.log('hi');
};
return clone; // 返回这个对象
}
const person = {
name: 'Nicholas',
friends: ['Shelby', 'Court', 'Van'],
};
const anotherPerson = createAnother(person);
anotherPerson.sayHi(); // hi
console.log(anotherPerson.name); // Nicholas
console.log(anotherPerson.friends); // [ 'Shelby', 'Court', 'Van' ]
function inheritPrototype(subType, superType) {
subType.prototype = Object.create(superType.prototype);
subType.prototype.constructor = subType;
}