Examples of methods and anonymous methods in JavaScript _javascript tips

Source: Internet
Author: User

The examples in this article describe the methods and anonymous methods in JavaScript. Share to everyone for your reference. The specific analysis is as follows:

JavaScript methods (Functions)

declaring functions

Starts with a function, followed by the name of the functions, which, unlike C #, Java, does not need to declare a return value type, a parameter type. No return value is undefined.

For example, it's more clear:
No parameter has no return value method:

Function F1 () {
alert (' This is a method ');
}
F1 ()//calling method 

No parameters have a return value method:

function F2 () {return
;
}
var result=f2 ()//declares a variable to receive the return value alert (result) in F1 ()
;//100 

Methods with parameters that return a value:

function F3 (n1,n2) {return
n1+n2;
}
var result=f3 (20,30);
alert (result);//50

Note 1:

First look at the example:

Function F1 () {
  alert (' This is a method '); 
}
Alert (F1 ()),//pop-up "This is a method", also displays the undefined

Reason: JS, the variable is not assigned value, is undefined; the chestnut F1 () does not return a value, then is an unknown value (undefined), where the unknown variable into alert (), of course, the pop-up is undefined

NOTE 2:

Alert (F1);//without parentheses, the entire code of F1 is displayed as a string:
function F1 () { 
alert (' This is a method '); 
} 

There is no method overload in JavaScript

Only the most recently defined methods are invoked:

Function F1 (n1,n2) { 
alert (N1+N2); 
} 
Function F1 (n1,n2) { 
alert (N1-N2); 
} 
F1 (10,2);//8

Conclusion: Only the most recently defined methods are invoked, regardless of where they are invoked.

Note: Digital +undefined=undefined

Function F1 (N1,N2,N3) { 
alert (N1-N2+N3); 
} 
F1 (10,2);
NaN, because no value is given to N3, N3 is undefined,
number plus undefined or undefined

The above conclusion: there is no method overload in JavaScript

Note When defining methods:

Custom function names do not duplicate the built-in methods:
Do not and JS built-in, Dom built-in method duplicate name, such as SelectAll, focus and other function names do not use.

Do not duplicate the system function. (There is a problem in calling your own defined focus method in the Click event.) Has the same name as the focus () method of the system)

Writing rules parentheses Note:

Generally in JS write curly braces are directly followed in the back

Function F1 () { 
return 
{ 
age:100}; 
} 
var s=f1 (); 
alert (s.age);
Undefined s turns out to be undefined,undefined.age or undefined.

anonymous method (very much)

Why is the anonymous method recommended?

1, there is a method in the 1.js function aa () {alert{' I'm pretty Handsome '}}

2, there is a method in the 2.js function aa () {alert{' I'm getting Better-looking '}}

3, the 1.js and 2.js into the index.html in turn, call AA (); The results show: I'm getting better and prettier.

Conclusion: the AA () method in 2.js will cover the AA () in 1.js.

What to do? The method name is no longer specified, using the anonymous method

Let's look at an example of assigning an anonymous method to a variable:

var ff=function (n1,n2) {return 
n1+n2; 
}; 
Alert (FF (20,30));//50

One line finishes writing anonymous methods:

Copy Code code as follows:
(function (N1,N2) {alert (N1+N2);}) (9,9);

Small cases: 1:

var x=1; 
var y=0; 
var z=0; 
var add=function (n) {n=n+1;return n}; 
Y=add (x);//result is 2, first call above add 
add=function (n) {N=n+3;return n;}; 
Z=add (x);//The result is 4, the call to the adjacent Add 
alert (y+ ', ' +z) above;//2,4

Small Case 2:

function AA () 
{ 
  alert ("AAA"); 
  return function () {alert ("BBB");} 
alert (AA);/without parentheses, the entire code of the AA method is displayed with 
alert (AA ());//aaa,function () {alert ("BBB"); AAA does not explain, and the following string is displayed as a return value of AA () 
alert (AA () ());//aaa,bbb,undefined 
//below decompose this sentence to explain 
var s=aa ();//aaa 
alert (S ());//s () is the function () {alert ("BBB");}; First to eject BBB, followed by the method does not return the value, so pop-up undefined

I hope this article will help you with your JavaScript programming.

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.