Javascript function objects, this keyword points to wrong object

前端 未结 4 1242
梦谈多话
梦谈多话 2021-01-19 19:26

I\'ve got a problem concerning the javascript \"this\" keyword when used within a javascript functional object. I want to be able to create an object for handling a Modal po

4条回答
  •  礼貌的吻别
    2021-01-19 19:57

    try this:

    CreateItemModal.prototype.show = function() {
        var me = this;
        this.$wrapper.dialog({
            buttons: {
                // this crashes because this is not the current object here
                Cancel: me.close
            }
        });
    };
    

    The reason why it doesn't work, because the "this" is referring to the dialog, not to that class.

提交回复
热议问题