今天看jquery发现其框架参数传递很有意思(1.4.0以上版本)
(function( window, undefined ) {
//中间代码
})(window);
很明显执行的时候传入的参数少一个,查了资料原来是为了屏蔽老浏览器(据说IE5之前)undefined被用户重定义,于是我试了下
var undefined = "hello world";
var a;
if (a == undefined){
console.log("ok2");
}
console.log(a);
console.log(undefined);//输出 undefined
输出:
ok2
undefined
undefined
很明显我最新的chrom中普通undefined已经不能被重定义了
但是无聊的我发现匿名函数中依然可以
(function(window,undefined){
var undefined = "hello world";
var a;
if (a == undefined){
console.log("ok1");
}
console.log(a);
console.log(undefined);//输出hello world
})(window);
输出:
undefined
hello world
结论:匿名函数判断变量undefined有风险,想判需谨慎。
原文:http://my.oschina.net/u/861926/blog/420487