Javascript function with prototype within parent function prototype

后端 未结 1 880
旧时难觅i
旧时难觅i 2021-01-26 06:10

Is this even possible?

function foo() {
    // do stuff
}
foo.prototype = {
    // stuff...
    bar: function() {
        // do some things with this, where this         


        
1条回答
  •  滥情空心
    2021-01-26 06:26

    No. You'd need to use

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

    Although this won't work. There is no reason to put the bar constructor on foos prototype, because when instantiating bar objects by using new ((new foo()).bar)(), there will be no reference to the foo instance. You could equally use new foo.prototype.bar().

    0 讨论(0)
提交回复
热议问题