【问题标题】:Javascript function with prototype within parent function prototype父函数原型中带有原型的 Javascript 函数
【发布时间】:2014-01-02 15:46:12
【问题描述】:

这可能吗?

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

【问题讨论】:

  • 是的,我遇到了一些基本问题。我不知道去哪里所以我在这里问:)

标签: javascript function object prototype


【解决方案1】:

没有。你需要使用

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

虽然这行不通。没有理由将bar 构造函数放在foos 原型上,因为当使用new ((new foo()).bar)() 实例化bar 对象时,将不会引用foo 实例。你同样可以使用new foo.prototype.bar()

【讨论】:

  • 这使它更清楚了。我认为我以错误的方式处理这个问题。
猜你喜欢
  • 1970-01-01
  • 2011-01-27
  • 1970-01-01
  • 2014-10-23
  • 2018-05-25
  • 2014-05-08
  • 1970-01-01
  • 2016-10-04
  • 1970-01-01
相关资源
最近更新 更多