Home > Web Front-end > JS Tutorial > body text

JavaScript global variable encapsulation module implementation code_javascript skills

WBOY
Release: 2016-05-16 17:47:46
Original
1252 people have browsed it

The code below is my test code, the comments are very important:

Copy the code The code is as follows:

/*global window,jQuery,validate_email,masterUI,$,rest*/
/**Enable ECMAScript "strict" operation for this function. See more:
* http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/
* http://stackoverflow.com/questions/5020479/what-advantages-does-using-functionwindow-document-undefined-windo
* Q1: Why are window and document being fed instead of just being accessed normally?
* A1: Generally to fasten the identifier resolution process, having them as local variables can help (although IMO the performance improvements may be negligible).
* A2: Passing the global object is also a widely used technique on non-browser environments, where you don't have a window identifier at the global scope, e.g.:
* (function (global) {
* //..
* })(this); // this on the global execution context is the global object itself
* A3: Passing window and document allows the script to be more efficiently minified
*
* Q2: Why the heck is undefined being passed in?
* A1: This is made because the undefined global property in ECMAScript 3, is mutable, meaning that someone could change its value affecting your code, for example:
* undefined = true; // mutable
* (function (undefined) {
* alert(typeof undefined); // "undefined", the local identifier
* })(); // <-- no value passed, undefined by default
* If you look carefully undefined is actually not being passed (there's no argument on the function call),
* that's one of the reliable ways to get the undefined value, without using the property window.undefined.
*
*/
(function(window, document, undefined) {
"use strict";
window.test = {
init: function () {
"use strict";
alert("ok");
}
};
})(window , document);// no undefined parameter here to avoid using mutable window.undefined changed by other guy

1. Instructions, refer to an article and a post on stackoverflow
2. (function(){})() This kind of code is written in a separate js file. When the js file is loaded by html, the function will be executed. The windows.text object is actually created.
In the future, the html code can call the method in the form of test.init.
The test html part of the code is as follows:
Copy code The code is as follows:

[plain] view plaincopyprint?

AppEngine SDK



< script type="text/javascript" src="../../master/plugin/artDialog4.1.6/jquery.artDialog.js">




3.Jslint will report Two questions. One is about undefined. I haven’t found any good method, so just let it complain. The final calling method of another format should be changed to:
Copy code The code is as follows:

[javascript ] view plaincopyprint?}(window, document)); }(window, document));

It doesn’t matter, just let it go. As long as it functions properly.
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!