0
我一直認爲當從另一個函數繼承原型時,Object.create(basefunc.prototype)
就是要走的路。另一種選擇是,其中調用基礎構造函數的缺點。在JavaScript中繼承時實例化函數有什麼意義?
但簡單設置thefunc.protoype = basefunc.prototype
的缺點是什麼?
考慮下面的代碼片段:
function foo(){
}
foo.prototype.blah = 1;
function bar(){
}
bar.prototype = foo.prototype; // instead of Object.create(foo.prototype)
// or new foo()
bar.prototype.constructor = bar;
bar.prototype.bleh = 1;
console.log(new bar()); // bar {blah: 1, bleh: 1}