可能重複:
How do I make a callable JS object with an arbitrary prototype?做一個javascript函數繼承原型
比方說,我們有我們可以在自己的範圍內單獨調用多個單獨的功能;但是他們也繼承了一些其他對象的原型。就像這樣:
//Here is the parent object:
var Human = function(){
this.isAlive = true;
};
Human.prototype.say = function(what){
alert(what + '!');
};
//These will inherit from it:
var ninja = function() {
alert("I'm a ninja!");
}
var samurai = function(){
alert("I'm a samurai!");
}
//Now, how can I make ninja and samurai behave like this:
ninja(); //I'm a ninja!
samurai(); //I'm a samurai!
ninja.say('Hello'); //Hello!
//And they should keep their inheritance. Like:
Human.prototype.die = function(){
this.isAlive = false;
}
ninja.die();
ninja.isAlive == false;
samurai.isAlive == true;
換句話說,是有辦法有繼承另一個對象的原型,但仍作爲調用函數的兩個對象?
注意:我要在Adobe ExtendScript中使用它(又名支持JavaScript),它不知道現代JavaScript。像,Object.defineProperty不起作用。那麼,有沒有一個正常的,標準的方法來做到這一點?
我不認爲你可以做到這一點沒有聲明'新忍者()',但也許有人可以證明我錯了。 – lbstr
+1好找 - 我會說那個答案證明我錯了 – lbstr
@apslillers是的,我不這些都是相同的問題。 – Aria