With a Javascript IFFE getting a Uncaught TypeError is not a function

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 is not a function

Fiddle https://jsfiddle.net/MillerDev/5qmnqr6q/

What is causing it?

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

data (currently placeholder)

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

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

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

Answers 1

  • dM is not a constructor function therfore you can not use "this" as a refrance to dM

    you need to 'new' the function like this

    function ReportGroupDataManager(){
    this.getResources = function(){
      return object
    }
    }
    
    let reportGroupDataManager = new ReportGroupDataManager()
    
    (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);


Related Articles