内部的JavaScript原型"立即调用函数表达式"

时间:2014-02-10 15:01:00

标签: javascript prototype

我在立即调用函数表达式(IIFE)中编写函数。

(function(){
  "use strict";
  function EventService(){
    //MASTER CLASS
  }
  EventService.prototype.eventObj = function(){
    var ES = getEvwentMapping();
    return EventService;
  };
  EventService.prototype.popup = function(){
    eventService.eventObj.createPopup();
  };
  EventService.prototype.drilldown = function(){
    eventService.eventObj.createDrilldown();
  };
})();


eventService();

由于 eventService()未定义,我收到错误。

3 个答案:

答案 0 :(得分:5)

您需要应用返回构造函数的模块模式。

var EventService = (function(){

   "use strict";

   function EventService(){
    //MASTER CLASS
   }

   EventService.prototype.eventObj = function(){
     var ES = getEvwentMapping();
     return ES;
   };

  EventService.prototype.popup = function(){
    this.eventObj().createPopup();
  };

  EventService.prototype.drilldown = function(){
    this.eventObj().createDrilldown();
  };

  return EventService;
})();

然后您可以通过执行

来简单地调用该方法
var eventService = new EventService();
eventService.popup();

问题是你试图在JavaScript闭包的帮助下应用一种封装;除非您明确导出它们,否则无法从外部访问闭包中定义的任何内容。

但是,你肯定在这里做错了;正如@Matt Burland所说,你不需要在这里使用IIFE。当您想要隐藏某些变量时,您可以从IIFE中受益,这样您就不会污染全局范围,或者您根本不想让它们可访问。我在你的代码中看不到这个意图。

答案 1 :(得分:4)

是的,您的功能已包含在您的IIFE中,不会成为任何外部范围的一部分。如果您在函数之外需要它,那么尝试返回它并将其分配给变量。例如:

var eventService = (function(){
    "use strict";
    function eventService(){
         //MASTER CLASS
    }
    eventService.prototype.eventObj = function(){
        var ES = getEvwentMapping();
        return EventService;
    };
    eventService.prototype.popup = function(){
        eventService.eventObj.createPopup();
    };
    eventService.prototype.drilldown = function(){
        eventService.eventObj.createDrilldown();
    };
    return eventService;
})();

如果您需要公开几个函数和/或属性,@ poliani建议的模块patern也很好。

然而,另一个问题是,为什么你认为你在这里需要一个IIFE?只需完全删除IIFE即可。

以下是你如何使用这个东西:

var myES = new eventService();
myES.popup();

http://jsfiddle.net/k7ZJY/

编辑:正如我之前所说,没有理由在这里实际使用IIFE,因为你没有隐藏任何东西而且你的代码实际上并没有执行任何东西。但是,你可以使用的一种方法是创建一个单例,我想知道这是否是你的实际目标。你能做的是:

var eventServiceSingleton = (function(){
    "use strict";
    function eventService(){
         //MASTER CLASS
    }
    eventService.prototype.eventObj = function(){
        var ES = getEvwentMapping();
        return EventService;
    };
    eventService.prototype.popup = function(){
        eventService.eventObj.createPopup();
    };
    eventService.prototype.drilldown = function(){
        eventService.eventObj.createDrilldown();
    };
    return new eventService();
})();

现在,您已将完全构造的eventService分配给变量eventServiceSingleton,并且无法创建另一个eventService对象。所以现在:

 eventServiceSingleton.popup();   // works

但:

 var anotherEventService = new eventService();    // eventService is undefined

答案 2 :(得分:1)

使用IIFE的主要原因是因为你想留下最小的空间 - 没有留下不必要的变量。

如果你只想调用一次eventService,那么你只需要在IIFE结束时调用eventService。

(function(){
  "use strict";
  function eventService(){
    //MASTER CLASS
  }
  eventService.prototype.eventObj = function(){
    var ES = getEventMapping();
    return eventService;
  };
  eventService.prototype.popup = function(){
    eventService.eventObj.createPopup();
  };
  eventService.prototype.drilldown = function(){
    eventService.eventObj.createDrilldown();
  };
  eventService(); // This is all you need to invoke just once
})();

但是,如果要保存对eventService函数的引用以多次调用它,则需要将其作为IIFE的返回值。

var eventService = (function(){
  "use strict";
  function eventService(){
    //MASTER CLASS
  }
  eventService.prototype.eventObj = function(){
    var ES = getEventMapping();
    return eventService;
  };
  eventService.prototype.popup = function(){
    eventService.eventObj.createPopup();
  };
  eventService.prototype.drilldown = function(){
    eventService.eventObj.createDrilldown();
  };
  return eventService; // return the function for future invocations
})();

eventService();
相关问题