Summary of common precautions for defining JavaScript Functions and summary of javascript Functions
This article summarizes the common problems of javascript function definition. Contains Common Errors for beginners. Share it with you for your reference. The details are summarized as follows:
1. at the same time, the JS engine also defines a variable with the same name as the function name. We actually use this variable when calling this function, and it can be called before the function declaration, for example
Foo (); // a function variable function foo () {alert ('hello') is actually used here ');}
2. function expression. At this time, the anonymous function is assigned to a variable, which must be used after definition. For example:
Foo (); // error reported. var foo = function () {alert ('hello') is not defined ');}
3. function expression (with function name), which should be avoided. In this case, the function name is only available internally in non-ie browsers. For example:
Bar (5); // an error is reported. The value var bar = function foo (n) {if (n = 1) return 1 is not defined; else return n * foo (n-1);} foo (5); // non-IE error, bar (5) not defined; // correct
4. Use Function to construct Function definitions. This method is inefficient. The Function body is parsed once each time a Function is executed. In addition, the declared function does not inherit the scope of the current declared position. By default, it only has a global scope, for example
Function foo () {var bar = 'hello'; return Function ('alert (bar) '); // error, global variable bar undefined} foo ()();
I believe this article provides some reference for you to learn javascript WEB programming.
Javascript function definition problems
A function is also an object. The following is an object of the definition type "people:
// Construction method with Parameters
Function people (name ){
This. name = name;
}
// Add an attribute (method) to the object)
People. prototype. getName = function (){
Return this. name;
}
// Instantiate an object
Var me = new people ('chenxiaowen ');
// Call the example method me. getName ()
Alert (me. getName ());
Question about javascript-defined functions? See the following example.
The parentheses indicate that the function is run;
This is a JS syntax. If you break down a program, you will naturally understand its principles:
Take your program as an example. If there is no special character behind it:
ApplicationConfig = function (){
Var modulePath = 'scripts/system/modules /';
Return {
GetModulePath: function (){
Return modulePath;
}
}
};
So what is stored in ApplicationConfig?
Is this function object function?
Then, run the following statement:
ApplicationConfig = ApplicationConfig ();
At this time, has the result of function () {...} been changed to the result of the ApplicationConfig variable?
When the syntax such as ApplicationConfig () appears, it indicates the execution of the function above the brackets, that is, ApplicationConfig:
Function (){...}()
What is the difference between this usage and the variable being assigned with brackets? But this function () {...} is only one-time, right?
So your routine:
ApplicationConfig = function (){
Var modulePath = 'scripts/system/modules /';
Return {
GetModulePath: function (){
Return modulePath;
}
}
}();
It is equivalent to the following program results:
ApplicationConfig = function (){
Var modulePath = 'scripts/system/modules /';
Return {
GetModulePath: function (){
Return modulePath;
}
}
};
ApplicationConfig = ApplicationConfig ();