JS About (function (window, undefined) {}) (window) The understanding of the "go"

Source: Internet
Author: User

JS about [function (window, undefined) {}] (window) writing comprehension [Network collation]

(Function (window, undefined) {}) (window); Why would you pass the window and the undefined as arguments to it? (function ($, undefined) {}) (JQuery); Similarly, because ECMAScript executes the JS code from the inside out, the global variable window or jquery object is passed in, avoiding the outer layer 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:  var undefined = 8;   (function (window) {      alert (window.undefined);//8      alert (undefined); 8  }) (window);    JS code:  var undefined = 8;   (function (window, undefined) {      alert (window.undefined);  //8      A Lert (undefined); Here the undefined parameter is a local name undefined variable with a value of undefined  }) (window);   Differentiate between the two ways: the first one is to look for 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 network http://www.software8.co/wzjs/javascript/2525.html   

In jquery we often see the following code:
;(function ($, window, document, undefined) {

function Body Specific code

}) (JQuery, window,document);

First of all, there are a few things that are worth advocating:
1, the front of the code, the semicolon, you can prevent multiple file compression merged to the last line of other files without a semicolon, resulting in a combined syntax error.
2, anonymous functions (function () {});: Because JavaScript execution expressions are from inside the parentheses to the outside, you can enforce the declared function with parentheses. Avoid variable conflicts within and outside the function.
3, $ argument: $ is a shorthand for jquery, many methods and class libraries also use $, here $ accept jquery objects, also to avoid the $ variable conflict, to ensure that plug-ins can run normally.
4, window, the document real parameter to accept window, Document object, window, document object is the global environment, and in the function of window, document is actually local variables, not the global window, The Document object. One of the benefits of this is that you can improve performance and reduce the query time of the scope chain, which is necessary if you need to call the window or document object multiple times in the body of the function to pass the window or document object as a parameter. Of course, if your plugin does not use these two objects, then you do not have to pass these two parameters.
5, finally left a undefined shape parameter, then this shape parameter is what use, look is a bit superfluous. 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

JS About (function (window, undefined) {}) (window) The understanding of the "go"

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.