使用Javascript IFFE获取未捕获的TypeError不是一个函数

使用Javascript IFFE获取未捕获的TypeError不是一个函数

问题描述:

我试图在javascript中编写一些匿名函数IFFE的东西,我不明白为什么我收到此错误

I was trying to code up some anonymous function IFFE stuff in javascript and I don't understand why I am getting this error

Uncaught TypeError:dM.getResources不是函数

小提琴 https://jsfiddle.net/MillerDev/5qmnqr6q/

造成这种情况的原因是什么?

What is causing it?

reportGroupDataManager (通常这是reportGroupDataManager.js文件)

reportGroupDataManager ( normally this is reportGroupDataManager.js file)

var reportGroupDataManager = (function() {
   var self = this;
   // cannot do this below   as dM.getResources is not a function
   //self.getResources = "blah";

  self.getResources = function() {
    return object;
     // return ajaxHelper.get(actions.adminReports.getResourceFileUrl, {});
  };

  console.log('reportGroupDataManager');

  return self;
});

数据(当前占位符)

function data() {
   console.log('in data'); 
}

IFFE

(function(jQ, dM, data) {
    var self = this;

    var initializePage = function () {
       console.log('in init');
    };

    dM.getResources()
        .done(initializePage, function(result) {
            console.log('in fx');
            console.log(result);
            console.log(result.CannotDeleteWithChild);
        });

})($, reportGroupDataManager, data);

通常 dM.getResources()是要从reportGroupDataManager获取数据,其中结果是一个对象

So normally dM.getResources() is going to fetch data from the reportGroupDataManager in which result is a object

但是使用此代码我不确定为什么错误

but with this code i'm not sure why the error

与上面相同的小提琴 - > https ://jsfiddle.net/MillerDev/5qmnqr6q/

same Fiddle as above --> https://jsfiddle.net/MillerDev/5qmnqr6q/

reportGroupDataManager 是构造函数,而不是对象。但是你期望一个由 reportGroupDataManager 创建的对象。因此,IIFE应该是这样的:

reportGroupDataManager is a constructor, not an object. But you're expecting an object created by reportGroupDataManager. Therefore the IIFE should be this:

(function(){
   // ...
})($, new reportGroupDataManager(), data);