Function objects in javascript

Source: Internet
Author: User
In javascript, there are four methods to declare a function. One of the following codes represents one.

1 function func1 (...) {...}
2 var func2 = function (...) {...};
3 var func3 = function func4 (...) {...};
4 var func5 = new Function ();

The first is our common function declaration method. The function name is func1. The difference between the second and third is that the second is an anonymous function, and the third is not. The fourth method is object declaration, because every function in javascript is an object. 1 // the two statements are equivalent.
2 function func1 (a, B ){
3 return a + B;
4}
5 var func1 = new Function ("a", "B", "return a + B ");

There is a difference between an anonymous function and a name function in javascript. Let's look at the code. 1 // declare an anonymous Function
2 func1 ();
3 var func1 = function (){
4 alter (1 );
5}

Run firefox
Firbug says func1 is not defined
See another method.

1 func1 ();
2 function func1 (){
3 alert (1 );
4}

It can be seen that although JavaScript is an interpreted language, it checks the entire
Whether the corresponding function definition exists in the Code. This function name is valid only when defined in the form of function funcName (), not anonymous function.

An advantage of anonymous function naming is to prevent function renaming.There are anonymous functions in javascript that can be called immediately after declarationThe Code is as follows.

1 var I = function (a, B ){
2 return a + B;
3} (1, 2 );

Here I = 3 is the return value rather than the function, because () has a higher priority than =
The extension in jquery is implemented in this way. Let's look at the code.

1 (function ($ ){
2
3 $. fn. select = function (select ){
4 if (select = undefined) select = true;
5 return this. each (function (){
6 var t = this. type;
7 if (t = 'checkbox' | t = 'Radio ')
8 this. checked = select;
9 else if (this. tagName. toLowerCase () = 'option '){
10 var $ sel = $ (this). parent ('select ');
11 if (select & $ sel [0] & $ sel [0]. type = 'select-one '){
12 // deselect all other options
13 $ sel. find ('option'). select (false );
14}
15 this. selected = select;
16}
17 });
18 };
19
20}) (jquery)

Note that you can directly write
Function (a, B ){
Return a + B;
} (1, 2)
A syntax error is reported.

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.