这可能吗?
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
s 原型,因为当使用实例化 bar 对象时new ((new foo()).bar)()
,将不会引用 foo 实例。你同样可以使用new foo.prototype.bar()
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)