簡體   English   中英

在對象中引用自身的匿名功能鍵Javascript

[英]In Object refer to itself anonymous function key Javascript

我有這個對象:

var crudConfig = function($wizard, $formModal, $deleteModal) {

'use strict';

return {


    handleOnShowFormModal : function() {

        $formModal.on('show.bs.modal', function(event) {
               ...................
                    this.fillForms(data);
               ....................
        });

        return this;
    },
    fillForms : function(data) {
        //do stuff
        return this;
    }
 }
}

當我用參數調用fillForms時出現問題。

Uncaught TypeError: this.fillForms is not a function

由於fillForms鍵是一個匿名函數,如何從對象內部調用它? 在其他相關問題上,我僅發現如果鍵具有字符串值並且我這樣調用,則如何引用自身: this.fillForms

this在回調中引用$formModal元素。 你需要做的就是保存this引用對象的變量的事件監聽器被調用,並使用回調中的變量來訪問對象之前。

像這樣:

handleOnShowFormModal : function() {
  var _this = this
  $formModal.on('show.bs.modal', function(event) {
    _this.fillForms(data); 
  });

  return this;
},

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM