I have the followi开发者_如何学Gong code, where i'm unable to get a reference to the parent object inside OnKeyUp function. I understand that in the OnKeyUp method, "this" refers to the textbox. But how do i access the parent object so that var textboxID gets me the correct value ?
function $MyObject() {
this.Control = {
inputBox: "#inputBox1",
name: "Control1",
BindEvent: function () {
$(this.inputBox).keyup(this.OnKeyUp);
},
OnKeyUp: function () {
var textBoxID = this.inputBox;
alert(textBoxID);
}
}
}
$(document).ready(function () {
var object1 = new $MyObject();
object1.Control.BindEvent();
});
function $MyObject() {
var self = this.Control = {
inputBox: "#inputBox1",
name: "Control1",
BindEvent: function () {
$(self.inputBox).keyup(self.OnKeyUp);
},
OnKeyUp: function () {
var textBoxID = self.inputBox;
alert(textBoxID);
}
};
}
精彩评论