[英]Javascript : How to get reference to the parent object inside a child object
I have the following code, where i'm unable to get a reference to the parent object inside OnKeyUp function. 我有以下代码,无法在OnKeyUp函数内获取对父对象的引用。 I understand that in the OnKeyUp method, "this" refers to the textbox. 我了解在OnKeyUp方法中,“ this”是指文本框。 But how do i access the parent object so that var textboxID gets me the correct value ? 但是我如何访问父对象,以便var textboxID为我提供正确的值?
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);
}
};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.