有多少全局变量可以传递给IIFE包装器? [英] How many globals make sense to be passed to the IIFE wrapper?

查看:114
本文介绍了有多少全局变量可以传递给IIFE包装器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将大量全球价值传递给IIFE在什么程度上有意义?

To which extent does it make sense to pass plenty of global values to an IIFE?

常见的事情是,在我到处都可以看到的范围内(窗口,文档和未定义的)仅传递3. 但是...如果仅出于最小化的目的而在代码中使用了10次以上,是否应该通过更多次?

The common thing is just to pass 3 as far as I see everywhere (window, document and undefined). But... would it make sense to pass more if they are used more than 10 times in the code just for the fact of minification?

就我而言,我在代码中发现了14次全局变量Math.可以将其传递给IIFE以节省42个字节.在这种情况下,哪一个并不多,但是如果我们一点一点地将不同的全局变量相加,那么传递尽可能多的全局变量总是有意义的,对吧? (符号,对象,错误,日期,JSON ...)

In my case I found the global variable Math 14 times in the code. It would make sense to pass it to an IIFE in order to save 42 bytes. Which in this case is not a lot, but if we sum bit by bit different global variables, then it would always make sense to pass as many global variables as possible, right? (Symbol, Object, Error, Date, JSON...)

(function($, window, document, Math, undefined) {
    $.fn.mydemo = function() {

    };
}(jQuery, window, document, Math));

然后,为什么这不是常见的方法?

Then, why isn't this a common approach?

解释减少的42个字节:

To explain the 42 bytes of reduction:

  • 数学= 4个字符
  • 1个字符= 1个字节
  • 14倍数学= 56字节
  • 缩小后,数学将被替换为单个字符
  • 因为函数可以定义为function($,w,d,m,u)
  • 缩写词Math(m)的14个字符= 14个字节
  • 56-14 =减少42个字节

推荐答案

如果仅出于最小化的目的在代码中使用了10次以上,是否应该通过更多次?

would it make sense to pass more if they are used more than 10 times in the code just for the fact of minification?

如果您非常关心缩小,那么,为什么不呢?

If you care that much about minification, sure, why not?

常见的事情是,我在任何地方(windowdocumentundefined)都通过了3.

The common thing is just to pass 3 as far as I see everywhere (window, document and undefined)

,尽管您看到没有通过document或通过jQuery(别名为$) ,就像平常一样.当然,不仅涉及缩小,而且有关性能,并且您只关心windowdocument那个

Yes, altough you see not passing document, or passing jQuery (aliased as $), just as often. And of course it's not only about minification, but about performance, and you only care for window and document on that behalf.

传递尽可能多的全局变量总是有意义的,对吧?

it would always make sense to pass as many global variables as possible, right?

好吧,除非您不在代码中使用它们.在大多数代码中通常不需要SymbolObjectErrorDateJSONMath和其他 .而且开发人员不希望每次更改代码时都建议您建议这些字节数,因此,此IEFE样板将保持原样(而且,恕我直言,有很多货源).

Well, except you don't use them in your code. Symbol, Object, Error, Date, JSON, Math and the others are not needed that often in most code. And developers don't like to do those byte counts you are suggesting every time they change a bit of code, so this IEFE boilerplate just stays as it is (and imho there's much cargo cult to it).

如果您真的很在意,您可以让您的矿工自动执行此操作.

You would let your minifier do this automatically if you'd really care.

这篇关于有多少全局变量可以传递给IIFE包装器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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