// 属性是否来自原型
function isPrototypeProperty(obj, name) {
// 不是对象自身属性且存在于对象中
return !obj.hasOwnProperty(name) && (name in obj);
}
/**
* 组合继承
*/
function SuperType(name) {
this.name = name;
this.colors = ['red', 'green', 'blue'];
}
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);
}
var instance1 = new SubType("Nicholas", 29);
instance1.colors.push("black");
console.log(instance1.colors);
instance1.sayName();
instance1.sayAge();
var instance2 = new SubType("Greg", 27);
console.log(instance2.colors);
instance2.sayName();
instance2.sayAge();
/**
* 原型式继承
*/
function object(o) {
function F() {}
F.prototype = o;
return new F();
}
const a = { name: 'mimi', age: 10 };
const b = Object.create(a, { name: { value: 'momo', configurable: true} });
console.log(b.name);
delete b.name;
console.log(b.name);
console