JS (function (window, undefined) {}) (window) Writing

Source: Internet
Author: User
JS's understanding of (function (window, undefined) {}) (window) writing [network arrangement] (function (window, undefined) {}) (window); this, why should we pass window and undefined as parameters to it? (Function ($, undefine... JS's understanding of (function (window, undefined) {}) (window) writing [network arrangement] (function (window, undefined) {}) (window); this, why should we pass window and undefined as parameters to it? (Function ($, undefined) {}) (jQuery); similarly, because ecmascript executes JS code from the inside out, it transmits the global variable window or jQuery object, this avoids searching for the outer layer and improves efficiency. Undefined is not supported in older browsers. If it is used directly, an error is reported. The js framework must consider compatibility. Therefore, an undefined parameter is added. Also, do not pass the window. undefined parameter to the form parameter. It is possible that the window. undefined has been modified by others. It is best to do nothing, and the undefined of the form parameter is actually undefined. Js Code: var undefined = 8; (function (window) {alert (window. undefined); // 8 alert (undefined); // 8}) (window); with Js Code: var undefined = 8; (function (window, undefined) {alert (window. undefined); // 8 alert (undefined); // The undefined parameter here is the local name of the undefined variable, and the value is undefined}) (window); Distinguish the above two methods: the first method is to find a window for each statement. The second method is to pass the window as a parameter. Yes, instead of finding the window for every statement, it should improve the efficiency. Therefore, even if undefined is defined by others, the undefined is still unaffected. It is probably to prevent the external variable definition from affecting the encapsulated interior to the greatest extent. For the original article, refer to the webmaster network. http://www.software8.co/wzjs/Javascript/2525.htmlJs Code: // method 1 (function (undefined) {window. property1 = ......; Window. property2 = ......; ...... }) (); // Method 2 (function (window, undefined ){... // code goes here}) (window); // method 3 (function (undefined) {var tmp = window; tmp. property1 = ......; Tmp. property2 = ......; ...... }) (); Method 1 has the lowest efficiency, and method 2 should be similar to method 3. Pass window as a parameter so that the statements in the code can directly use the window in the parameter, instead of looking for the outermost object. If you want to set another 100000 properties for window in the function, you only need to find the outermost layer object once after passing the parameters. You do not need to pass parameters. All the statements that use window need to find the outermost layer object.
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.