js插件
为何在插件设置形参undefined
In ECMAScript 3, undefined is mutable. Which means its value could be reassigned, something like undefined = true; for instance, oh my! Thankfully in ECMAScript 5 strict mode (‘use strict’;) the parser will throw an error telling you you’re an idiot. Before this, we started protecting our IIFE’s by doing this:
(function (window, document, undefined) {
})(window, document);
Which means if someone came along and did this, we’d be okay:
undefined = true;
(function (window, document, undefined) {
// undefined is a local undefined variable
// true
})(window, document);
Correct Mistake - HKK
实际上,ES5 中 undefined 为 window 属性 实际上为数据的基本类型
无论是否为 strict mode ,undefined 都无法更改
所以总结如下:
形参undefined 作用为 兼容 – 另外,减少undefined 寻找,提高速度也是一方面。
让我们做一个实验
* 真的是非常精彩 *
// 1.1
undefined = true;
(function (window, document, undefined) {
console.log(undefined) //undefined
})(window, document);
//1.2
undefined = true;
(function (window, document, undefined) {
console.log(undefined) //2
})(window, document,2);
by HKK
引用
文章 - What (function (window, document, undefined) {})(window, document); really means