简体   繁体   English

如何正确使用立即调用函数表达式(IIFE)声明类?

[英]How to properly use Immediately-Invoked Function Expression (IIFE) to declare classes?

I understand there are many different ways to declare an IIFE: 我知道可以通过多种方式声明IIFE:
( http://benalman.com/news/2010/11/immediately-invoked-function-expression/ ) http://benalman.com/news/2010/11/immediately-invoked-function-expression/

But what I can't understand is how sometimes many classes are defined within the same IIFE, and other times they are separated in each their own. 但是我不明白的是,有时在同一IIFE中定义了多少个类,而有时它们又各自分开。

Example: 例:

This... 这个...

(function(scope) {
  var ClassA = scope.ClassA = function(params) {
    this.params = params;
  }

  var ClassB = scope.ClassB = function(params) {
    this.params = params;
  }
})(someNamespace || window);

VS. VS. That... 那...

(function(scope) {
    var ClassA = scope.ClassA = function(params) {
        this.params = params;
    }
})(someNamespace || window);

(function(scope) {
    var ClassB = scope.ClassB = function(params) {
        this.params = params;
    }
})(someNamespace || window);

Are there any advantages to isolate each classes into their own IIFE? 将每个类隔离到自己的IIFE中是否有任何优势?

The point of using an IIFE is to stop variables used by one piece of code leaking into places where they might get mixed up with identically named variables used by another piece of code. 使用IIFE的目的是阻止一段代码使用的变量泄漏到可能与另一段代码使用的同名变量混淆的地方。

If the two functions should share variables, then putting them in the same IIFE is a good idea. 如果两个函数应该共享变量,那么将它们放在同一IIFE中是一个好主意。 If not, then they should be kept separate to avoid naming collisions and to show to the maintainer that they are separate. 如果不是,则应将它们分开,以避免命名冲突,并向维护者显示它们是分开的。

There are slight performance advantages to sharing the IIFE. 共享IIFE有一些性能优势。 I put your two examples into jsperf and compared them, and the shared IIFE runs slightly faster....but the performance gains are totally negligible unless you are declaring many thousands of classes in this way. 我将您的两个示例放入jsperf中并进行了比较,共享的IIFE的运行速度快....但是,除非您以这种方式声明了成千上万的类,否则性能提升几乎可以忽略不计。

http://jsperf.com/iife-performance-for-class http://jsperf.com/iife-performance-for-class

Here's a different test comparing IIFE performance in a slightly different manner: http://jsperf.com/iife-performance 这是一个以稍微不同的方式比较IIFE性能的测试: http ://jsperf.com/iife-performance

I prefer to put each class in its own file so that it's easier to find each class during the editing process, which would result in each class getting its own IIFE. 我更喜欢将每个类放在自己的文件中,以便在编辑过程中更容易找到每个类,这将导致每个类都具有自己的IIFE。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 立即调用函数表达式(IIFE)与否 - Immediately-Invoked Function Expression (IIFE) vs not 立即调用函数表达式(IIFE)在JavaScript中 - 传递jQuery - Immediately-Invoked Function Expression (IIFE) In JavaScript - Passing jQuery 立即调用函数表达式 (IIFE) 优于普通函数 - Benefit of Immediately-invoked function expression (IIFE) over a normal function 使用命名的立即调用函数表达式(IIFE)而不是注释 - Using Named Immediately-Invoked Function Expression (IIFE) instead of comments HotTowel角和立即调用函数表达式(IIFE) - HotTowel Angular and Immediately-Invoked Function Expression (IIFE) Object.Prototype方法和IIFE中的“使用严格”(立即调用的函数表达式) - Object.Prototype Methods and 'Use Strict' in an IIFE (Immediately-Invoked Function Expression) 编写代码,使用立即调用的函数表达式 (IIFE) 在指定的位置创建斐波那契函数 - Write code that uses an immediately-invoked function expression (IIFE) to create the fibonacci function where indicated 编写用户脚本时是否真的需要立即调用函数表达式 (IIFE) 模式? - Is the Immediately-Invoked Function Expression (IIFE) pattern really necessary when writing userscripts? 为什么立即调用函数表达式(IIFE)与自执行匿名函数不同? - Why Immediately-Invoked Function Expression (IIFE) are different from Self-Executing Anonymus Functions? XMLHttpRequest - 立即调用的函数表达式 - XMLHttpRequest - Immediately-invoked Function Expression
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM