我试图像这样声明一个JavaScript对象:
var Calculator = function() {
this.textbox = new Textbox({
...
});
this.btn1 = new Button ({
...
onClick: function() {
this.textbox.value += "1";
});
...
};
我做了一些调试,发现this
内的this.btn1
对象不再引用Calculator
对象。如何将textbox
引用到Calculator
的{{1}}?
非常感谢提前!
答案 0 :(得分:3)
var self = this
var Calculator = function() {
var self = this; // self is a reference to calculator, always.
this.textbox = new Textbox({
...
});
this.btn1 = new Button ({
...
onClick: function() {
self.textbox.value += "1";
});
...
};