【发布时间】:2011-12-04 07:59:17
【问题描述】:
我用 babel 对象做了一个简单的例子:
function babel(){
this.english = {
hello: function () { alert('hello'); },
goodbye: function () { alert('goodbye'); }
teeshirt: function () { alert('T-shirt'); }
}
}
现在,我想扩展这个对象:
babel.prototype.french = {
bonjour: function () { alert('bonjour'); },
aurevoir: function () { alert('au revoir'); }
}
但是如果我需要使用之前定义的现有函数呢?
babel.prototype.french = {
bonjour: function () { alert('bonjour'); },
aurevoir: function () { alert('aurevoir'); },
teeshirt: function () { this.english.teeshirt(); }
}
我能做的是:
var say = new babel();
(function (_this) {
babel.prototype.french = {
bonjour: function () { alert('bonjour'); },
aurevoir: function () { alert('aurevoir'); },
hello: function () { _this.english.hello(); }
}
})(say);
但在这种情况下,我将始终使用 say 对象的上下文,不是吗?
【问题讨论】:
-
您想让
var b = new babel(); b.french.teeshirt();提醒“T 恤”吗?还是其他一些行为? -
是的,这是我想要的行为,但此外,我想使用我的对象的上下文。
标签: javascript