Javascript:如何引用子对象内的父对象

时间:2022-02-04 16:53:48

I have the following 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 ?

我有以下代码,我无法获取OnKeyUp函数内的父对象的引用。据我所知,在OnKeyUp方法中,“this”指的是文本框。但是我如何访问父对象,以便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();

    });

1 个解决方案

#1


1  

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);
        }


    };
}

#1


1  

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);
        }


    };
}