模块不属于IIFEs [英] Modules that are not IIFEs
问题描述
请参阅模块数据依赖
我在我的应用程序几个模块依赖于数据来自服务器。我实现了模块,IIFEs,因为模块模式表明,但为了能够引用它们的回调Ajax请求我想确定他们作为普通的功能,并初始化它们的回调(参见答案其他职位供参考)。我到处看了看,模块模式包括IIFEs的。什么是使用他们作为普通的功能,我实例在AJAX回调的缺点(如果有的话)?那是一个很好的做法?
看一看从previous后下面的例子:
getAJAX(URL,功能(数据){
//写想从阿贾克斯数据的任何code。
}, 真正);
这code中含有的生活to函数调用。这里,它被称为
匿名
功能也。这是调用内联函数的方式 无关,与模块化的方法。
请参阅下面的类重presentation在javascript:
VAR类名=功能(数据,pubsubService){
变种项= [];
//公共职能
this.generateItems =功能(firstItemIndex,stopIndex){
VAR DATALENGTH = data.length;
stopIndex =(stopIndex<数据长度)? stopIndex:DATALENGTH;
项目= data.slice(firstItemIndex,stopIndex);
pubsubService.publish('itemsGenerated');
};
//私有函数
VAR getItems =功能(){
返回的项目;
};
返回 {
generateItems:generateItems,
getItems:getItems
};
};
这是一类,其中
generateItems
是一个公共职能和getItems
是一个私人的功能。
现在参照你previous岗位,而不是建立定期的功能,创建一个类模块
,其中包含该模块的方法,创建对象
和调用方法类似以下内容:
瓦尔OBJ =新的类名(数据,pubsubService);
obj.generateItems(firstItemIndex,stopIndex);
我觉得这可以帮助你理解这个概念。
请参考下面的链接继续探索更多:
http://book.mixu.net/node/ch6.html
<一个href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript" rel="nofollow">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript
<一个href="http://www.htmlgoodies.com/html5/tutorials/create-an-object-oriented-javascript-class-constructor.html#fbid=5m_t2A6hozg" rel="nofollow">http://www.htmlgoodies.com/html5/tutorials/create-an-object-oriented-javascript-class-constructor.html#fbid=5m_t2A6hozg
I have a few modules in my app which depend on data coming from the server. I implemented the modules as IIFEs, as the module pattern suggests, but in order to be able to reference them as the callback for the ajax request I am thinking of defining them as regular functions, and initialising them in the callback (see the answer in the other post for reference). Everywhere I looked, the module pattern consisted of IIFEs. What are the drawbacks(if any) of using them as regular functions which I instantiate in the AJAX callback? Is that a good practice?
Have a look at following example from previous post:
getAJAX(url, function(data){
// write any code that want data from Ajax.
}, true);
This code contains IIFE function call. Here it is called an
anonymous
function also. It is a way of invoking inline function and has nothing to do with Modular approach.
See following Class representation in javascript:
var ClassName = function(data, pubsubService) {
var items = [];
// public function
this.generateItems = function(firstItemIndex, stopIndex) {
var dataLength = data.length;
stopIndex = (stopIndex < dataLength) ? stopIndex : dataLength;
items = data.slice(firstItemIndex, stopIndex);
pubsubService.publish('itemsGenerated');
};
// private function
var getItems = function() {
return items;
};
return {
generateItems : generateItems,
getItems : getItems
};
};
This is a class where
generateItems
is a public function andgetItems
is a private function.
Now in reference to you previous post, instead of creating regular function create a class as module
which contains method of that module, create object
and call methods like following:
Var obj = new ClassName(data,pubsubService);
obj.generateItems(firstItemIndex,stopIndex);
I think this can help you understand the concept.
Refer following link to explore more:
http://book.mixu.net/node/ch6.html
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript
这篇关于模块不属于IIFEs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!