//第241页中部内容
function SuperType() {
this.property = true;
}
SuperType.prototype.getSuperValue = function() {
return this.property;
};
function SubType() {
this.subproperty = false;
}
SubType.prototype = new SuperType();
SubType.prototype.getSubvalue = function() {
return this.subproperty;
};
SubType.prototype.getSuperValue = function() {
return false;
}
let instance= new SubType();
console.log(instance.getSuperValue());
console.log(SuperType.getSuperValue);
console.log(instance.getSubvalue());
let person = new SuperType();
console.log(person.getSubvalue);
console.log(person.getSuperValue());
//apply()
function Person() {
this.name = "木木";
this.age = "21";
console.log(this);
console.log(this.sex);
console.log(this.name);
}
let obj = {
sex: '男'
};
Person.apply(obj);
console.log(obj.name);
console.log(obj);
console.log(Person.age);
function SuperTypes(name) {
this.name = name;
gets= function() {
console.log(this.name);
};
}
function SubTyper() {
SuperTypes.call(this, "nnnn");
this.age = 29;
}
let instances = new SubTyper();
let instanceza = new SubTyper();
console.log(instances.name);
console.log(instances.age);
instances.name = "dasd";
console.log(instances.name);
console.log(instanceza.name);
console.log(instance.gets);
// let instancez = new SuperTypes("da");
// console.log(instancez.gets);
//组合继承
function SuperType1(name) {
this.name = name;
this.colors = ["red","blue","green"];
}
SuperType1.prototype.sayName = function() {
console.log(this.name);
};
function SubType1(name , age) {
SuperType1.call(this, name);
this.age = age;
}
SubType1.prototype = new SuperType1();
SubType1.prototype.sayAge = function() {
console.log(this.age);
};
let instance1 = new SubType1("dada", 29);
instance1.colors.push("black");
console.log(instance1.colors);
instance1.sayName();
instance1.sayAge();
let instance2 =new SubType1("kkkk", 27);
console.log(instance2.colors);
instance2.sayName();
instance2.sayAge();
function object(o) {
function F() {}
F.prototype = o;
return new F();
}
let persons = {
name: "dasdd",
friends: ["s","d","f"]
};
let anotherPerson = object(persons);
anotherPerson.name = "dasssssss";
anotherPerson.friends.push("as");
console.log(persons.friends);
console.log(anotherPerson.name);
console.log(anotherPerson.friends);
let yetAnotherPerson = object(persons);
yetAnotherPerson.name = "jj";
yetAnotherPerson.friends.push("kk");
console.log(yetAnotherPerson.friends);
//寄生式组合继承
function SuperType4(name) {
this.name = name;
this.colors = ["red", "blue", "green"];
}
SuperType4.prototype.sayName = function() {
console.log(this.name);
};
function SubType2(name, age) {
SuperType4.call(this, name);
this.age = age;
}
SubType2.prototype = new SuperType4();
SubType2.prototype.constructor = SubType2;
SubType2.prototype.sayAge = function() {
console.log(this.age);
};
function inheritPrototype(subType3,superType2) {
let prototype = object(superType2.prototype);
prototype.constructor = subType3;
subType3.prototype = prototype;
}
function SuperType2(name) {
this.name = name;
this.color = ["red", "blue", "green"];
}
SuperType2.prototype.sayName = function() {
console.log(this.name);
};
function SubType3(name, age) {
SuperType2.call(this, name);
this.age = age;
}
inheritPrototype(SubType3, SuperType2);
SubType3.prototype.sayAge = function() {
console.log(this.age);
};
let test = new SubType3("d", 12);
console.log(test.color);
console.log(test.name);
test.color.push("dd");
console.log(test.color);
test.sayAge();
test.sayName();
let test2 = new SubType3("dd", 13);
console.log(test2.color);
console.log(test2.name);
console