簡體   English   中英

父函數原型中帶有原型的Javascript函數

[英]Javascript function with prototype within parent function prototype

這有可能嗎?

function foo() {
    // do stuff
}
foo.prototype = {
    // stuff...
    bar: function() {
        // do some things with this, where this refers to foo
    },
    bar.prototype: {
        // set some definitions for bar to work with.
        // Where does "this" go and what does it refer to?
    }
}

不,您需要使用

function bar() {...}
bar.prototype = {...};
function foo() {...}
foo.prototype.bar = bar;

雖然這行不通。 沒有理由將bar構造函數放在foo的原型上,因為通過使用new ((new foo()).bar)()實例化bar對象時,將不會引用foo實例。 您可以同樣使用new foo.prototype.bar()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM