For developers, most of the time we involve JavaScript , and in the process of using it, the most frustrating thing is that the variable doesn't have a corresponding scope of use.
In development, for any variable, array, function, object, etc., as long as it is not inside the function, it will be assumed to be global, meaning that the other script on the page can access it, and can overwrite overrides.
For variables that do not have to be placed inside the function, how do we ensure that the overrides are not accessed and overwritten by other code? At this point, you need to put the variable inside an anonymous function, immediately after the definition is called, for example, written in JavaScript as follows, will produce three global variables and two global functions:
var name = ' Chris ';
var age = ' 34 ';
var status = ' single ';
function Createmember () {
// [...]
}
function Getmemberdetails () {
// [...]
}
In the above code, we also need to pay attention to the problem of duplicate variable name, if this page other script also has a variable called status, in order to distinguish, we can encapsulate it in a myapplication:
var myapplication = function () {
var name = ' Chris ';
var age = ' 34 ';
var status = ' single ';
function Createmember () {
// [...]
}
function Getmemberdetails () {
// [...]
}
}();
However, there is no function outside of the function. If that's what you need, then it's okay. You can also omit the name of the function:
(function () {
var name = ' Chris ';
var age = ' 34 ';
var status = ' single ';
function Createmember () {
// [...]
}
function Getmemberdetails () {
// [...]
}
})();
If you want to use the inside of the function, you need to make some changes. To be able to access createmember () or getmemberdetails (), you need to turn them into MyApplication properties, exposing them to the outside world:
var myapplication = function () {
var name = ' Chris ';
var age = ' 34 ';
var status = ' single ';
return{
Createmember:function () {
// [...]
},
Getmemberdetails:function () {
// [...]
}
}
}();
Myapplication.createmember () and
Myapplication.getmemberdetails () now works.
This is called module mode or singleton. However, it is necessary to change the sentence pattern so that functions and variables can be accessed by the outside world or even add the MyApplication prefix. Seem to complicate simple things, many developers do not like to do so, but rather prefer simply to be able to be accessed by the external elements of the pointer. This simplifies the way outside calls are made:
var myapplication = function () {
var name = ' Chris ';
var age = ' 34 ';
var status = ' single ';
function Createmember () {
// [...]
}
function Getmemberdetails () {
// [...]
}
return{
Create:createmember,
Get:getmemberdetails
}
}();
Myapplication.get () and myapplication.create () now work.
The above is JavaScript development , anonymous functions and modular use, is a script variable use of a small skill, I hope to be able to you in the development of the script code to be helpful.
The anonymous function of JavaScript and how to use it in a modular way