//父類
function person()
} //子類
function man()
man.prototype = new person();
var one = new man();
console.log(one.feature); //['beard','strong']
console.log(one.hair); //black
console.log(one.eye); //black
console.log(one.skin); //yellow
console.log(one.view()); //black,black,yellow
//父類
function person()
} //子類
function man()
var one = new man();
console.log(one.feature); //['beard','strong']
console.log(one.hair); //black
console.log(one.eye); //black
console.log(one.skin); //yellow
console.log(one.view()); //black,black,yellow
//父類
function person()
} //子類
function man()
man.prototype = new person();
var one = new man();
console.log(one.feature); //['beard','strong']
console.log(one.hair); //black
console.log(one.eye); //black
console.log(one.skin); //yellow
console.log(one.view()); //black,black,yellow
//父類
function person(hair,eye,skin)
}
//子類
function man(hair,eye,skin)
//call方式
//子類
function man(hair,eye,skin)
經此種種,最好的繼承方式就是call+prototype方式了,之後你可以試一下(one instanceof baseclass)的值是否為true。
第三種繼承方式也有缺陷:子類new物件時要傳一遍父類所需的引數,而且會重現父類中的屬性和方法,下面這種繼承方式才是完善的:
function person(name)
person.prototype.getname = function()
function chinese(name, nation)
//繼承方法
function inherit(subclass, superclass)
f.prototype = superclass.prototype;
subclass.prototype = new f();
subclass.prototype.constructor = subclass.constructor;
} inherit(chinese, person);
chinese.prototype.getnation = function() ;
var p = new person('shijun');
var c = new chinese("liyatang", "china");
console.log(p); // person
console.log(c); // chinese
console.log(p.constructor); // function person(name){}
console.log(c.constructor); // function chinese(){}
console.log(c instanceof chinese); // true
console.log(c instanceof person); // true
js繼承方式及其優缺點?
原型鏈繼承的缺點 一是字面量重寫原型會中斷關係,使用引用型別的原型,並且子型別還無法給超型別傳遞引數。借用建構函式 類式繼承 借用建構函式雖然解決了剛才兩種問題,但沒有原型,則復用無從談起。所以我們需要原型鏈 借用建構函式的模式,這種模式稱為組合繼承 組合式繼承 組合式繼承是比較常用的一種繼承方法,...
js的三種繼承方式及其優缺點
第一種,prototype的方式 父類 function person 子類 function man man.prototype new person var one new man console.log one.feature beard strong console.log one.hair...
js的三種繼承方式及其優缺點
第一種,prototype的方式 父類 function person 子類 function man man.prototype new person var one new man console.log one.feature beard strong console.log one.hair...