父类
function Father(name) {
this.name= name
this.sleep = function() {
console.log(this.name + ' is sleep ');
}
}
Father.prototype.look = function(book) {
console.log(this.name + ' is look ' + book);
}
- 原型继承
function Son() {}
Son.prototype = new Father()
Son.prototype.constructor = Son
var son1 = new Son()
console.log(son1.name);
console.log(son1.sleep());
console.log(son1.look('一千零一夜'));
- Call
function Son(name) {
Father.call(this)
this.name = name
}
var son1 = new Son('zooey')
- 组合
function Son(name) {
Father.call(this)
this.name = name
}
Son.prototype = new Father()
Son.prototype.constructor = Son
var son1 = new Son(‘mike’)
console.log(son1.name);
console.log(son1.sleep());
console.log(son1.look(“童话”));
- 寄生组合
function Son(name) {
Father.call(this)
this.name = name
}
function createObj(obj) {
var f = function(){}
f.prototype = obj
return new f()
}
Son.prototype = createObj(Father.prototype)
Son.prototype.contructor = Son
var son1 = new Son('luyis')
console.log(son1.name);
console.log(son1.sleep());
console.log(son1.look('百年'));