js插件 - 为何在插件设置形参undefined?

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值