0

これも可能ですか?

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?
    }
}
4

1 に答える 1

0

いいえ。使用する必要があります

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

これは機能しませんが。を使用してbarオブジェクトをインスタンス化する場合、fooインスタンスへの参照がないため、barコンストラクターをプロトタイプに配置する理由はありません。同様に使用できます。foonew ((new foo()).bar)()new foo.prototype.bar()

于 2012-06-21T21:16:00.637 に答える