最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Passed in undefined argument in jQuery core source code - Stack Overflow

programmeradmin0浏览0评论

I noticed that in the jQuery core, one of the two arguments passed in is undefined.

(function( window, undefined ) {

    // Use the correct document accordingly with window argument (sandbox)
    var document = window.document;
    var jQuery = (function() {
        // ...defintion of the rest of the core...
    window.jQuery = window.$ = jQuery;
})(window);

Can anyone explain why the second argument is undefined?

Thanks in advance!

I noticed that in the jQuery core, one of the two arguments passed in is undefined.

(function( window, undefined ) {

    // Use the correct document accordingly with window argument (sandbox)
    var document = window.document;
    var jQuery = (function() {
        // ...defintion of the rest of the core...
    window.jQuery = window.$ = jQuery;
})(window);

Can anyone explain why the second argument is undefined?

Thanks in advance!

Share Improve this question asked Mar 22, 2011 at 17:19 maximusmaximus 2,4475 gold badges40 silver badges56 bronze badges 3
  • 3 Because undefined can be overwritten and this way it is guaranteed to be undefined... My opinion. – mhitza Commented Mar 22, 2011 at 17:21
  • 3 Because Resig wanted to see how many people he could get to ask about it on Stack Overflow. – Wayne Commented Mar 22, 2011 at 18:02
  • 1 possible duplicate of jQuery question: what does it really mean? – hjpotter92 Commented May 24, 2013 at 11:58
Add a ment  | 

2 Answers 2

Reset to default 2

Undefined is a type but is also a global variable.

You can have a module that overwrites the value of undefined by doing undefined = whatever.

jQuery uses a immediate function to scope window and undefined.

https://developer.mozilla/en/JavaScript/Reference/Global_Objects/undefined

Since someone could overwrite undefined like this

undefined = true;

Code of your immediate function will pass it as it should be (stay undefined). I think it's mentioned in 10 Things I Learned from the jQuery Source.

发布评论

评论列表(0)

  1. 暂无评论