Summary of common precautions for defining JavaScript Functions and summary of javascript Functions

Source: Internet
Author: User

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 ();

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.