下一个参数是' undefined'.在jQuery中,为什么? [英] Next parameter is 'undefined' in jQuery, why?

查看:51
本文介绍了下一个参数是' undefined'.在jQuery中,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下 jQuery 的代码中,为什么下一个参数是 undefined

In following code of jQuery, why next parameter is undefined

JavaScript:

JavaScript:

(function(a,b){
    ....
})(window)

这里 a = window b = undefined ,为什么会这样?

Here a=window but b=undefined, why is so?

推荐答案

这是一种常见的技术,可确保自 undefined 属性以来,您可以检查真实的 undefined 值窗口对象的以前曾经是可写的,因此不能可靠地用于检查.由于只有一个参数被传递给该函数,因此第二个参数被确保为 undefined .该变量的名称无关紧要,它可能是 undefined ,但也可能是 foobar ,或者在这种情况下(因为这是保存有价值的字节的最短方法) b .

That's a common technique to assure you have a true undefined value to check against since the undefined property of the window object used to be writeable and thus could not reliably be used in checks. Since only one parameter is handed over to the function, the second one is assured to be undefined. The name of that variable does not matter, it could be undefined but as well foobar or, as in this case, (because this is the shortest possible way saving valuable bytes) b.

现在您可以安全地检查变量的不确定性,因为您可以确定b的值:

Now you can safely check for undefinedness of variables, because you are sure about the value of b:

// overwriting the global undefined property works in old browsers
undefined = 3;

(function(a,b){

    var asd; // not initialised, thus undefined

    if (asd === undefined){
       // should trigger, but won't because undefined has been overwritten to 3
    }
    if (asd === b){
       // is safe, bcause you know that b is undefined
    }

})(window)

新的浏览器(IE9,FF4 +,Chrome)遵循EcmaScript5规范,并且 undefined 不再可写.

New browsers (IE9, FF4+, Chrome) obey the EcmaScript5 specification and undefined is not writable any more.

这篇关于下一个参数是' undefined'.在jQuery中,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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