"Go" in jquery Source (function (window, undefined) {}) (window)

Source: Internet
Author: User

(Function (window, undefined) {}) (window);
Well, why would you pass window and undefined as arguments to it?

(function ($, undefined) {}) (JQuery); Similarly

Because the ECMAScript executes the JS code from inside to outside, so the global variable window or jquery object is passed in, it avoids the outer to look for, improve efficiency. Undefined in the older generation of browsers is not supported, direct use will be error, JS framework to consider compatibility, so add a formal parameter undefined.

Also, do not use window.undefined to pass to the formal parameter, it is possible window.undefined be modified by others, the best is nothing to preach, formal parameter undefined is the real undefined.

JS Code:
    1. var undefined = 8;
    2. (function (window) {
    3. alert (window.undefined); //8
    4. alert (undefined); //8
    5. }) (window);

With JS code:
    1. var undefined = 8;
    2. (function (window, undefined) {
    3. alert (window.undefined); //8
    4. alert (undefined); ///Here The undefined parameter is a local name undefined variable with a value of undefined
    5. }) (window);

Distinguish between the two ways: the first one is to find a window for each of the statements. The second is to pass the window as a parameter, and not to go to the window for each statement, it should be more efficient. So the latter, even if the outside people to define the undefined, the inside of the undefined is still unaffected. Presumably to minimize the impact of external variable definitions on the internals of the package.

Original reference from webmaster net http://www.software8.co/wzjs/JavaScript/2525.html

JS Code:
  1. Way One
  2. (function (undefined) {
  3. Window.property1 = ...;
  4. Window.property2 = ...;
  5. ......
  6. })();
  7. Way Two
  8. (function (window, undefined) {
  9. ... //code goes here
  10. }) (window);
  11. Mode three
  12. (function (undefined) {
  13. var tmp = window;
  14. Tmp.property1 = ...;
  15. Tmp.property2 = ...;
  16. ......
  17. })();

The efficiency of mode one is obviously the lowest, the way two and the way three should be similar. Passing the window as a parameter allows the statement inside the code to use the window directly in the argument without having to find the outermost object. If you want to set 100,000 properties for the window in the function, passing the argument in the past only needs to find the outermost object once. Without argument passing, the statement using the window will have to find the outermost object.

function ($, undefined) {}) (jquery) circumvents the user's definition of undefined, resulting in an exception to the functions (function ($, undefined) {}) (jquery) if the user var undefined =x Iaoyan, (function ($, undefined) {}) (JQuery)//Here you can pass a undefined parameter, avoid the definition above, as long as the value is not passed in, undefined will not be assigned

"Go" in jquery Source (function (window, undefined) {}) (window)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.