使用Javascript IFFE获取未捕获的TypeError不是一个函数 [英] With a Javascript IFFE getting a Uncaught TypeError is not a function

查看:222
本文介绍了使用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);

这篇关于使用Javascript IFFE获取未捕获的TypeError不是一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆