jQuery中的下一个参数是“未定义",为什么? [英] Next parameter is 'undefined' in jQuery, why?

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

问题描述

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

In following code of jQuery, why next parameter is undefined

JavaScript:

JavaScript:

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

这里a=windowb=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.

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

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