var a = function(){
this.sayFoo = function(){
console.log('foo');
};
}
var b = function(){
console.log(this.prototype); //undefined
this.sayBar = function(){
console.log('bar');
};
}
b.prototype = new a();
var bInst = new b();
bInst.sayFoo();
bInst.sayBar();
console.log(b.prototype); //a {sayFoo: function}
http://jsfiddle.net/KbBny/1/內部構造函數原型設置
如何添加sayBar
到b
原型構造函數裏面?
b.prototype = new a();
是否覆蓋原型,或合併b
的a
的?
原型的整體思路是,你把它定義* *外的構造,而不是內部。 –
閱讀此:http://stackoverflow.com/a/8096017/783743 –