Examples of methods and anonymous methods in Javascript _ javascript skills

Source: Internet
Author: User
This article mainly introduces the methods and anonymous methods in Javascript, and analyzes in detail the definitions and usage skills of methods and anonymous methods in javascript in the form of examples, you can refer to the examples in this article to describe the methods and anonymous methods in Javascript. Share it with you for your reference. The specific analysis is as follows:

Javascript method (function)

Declare a function

It starts with a function and is followed by the function name. Unlike C # and java, Javascript does not need to declare the return value type and parameter type. Undefined is returned.

For example, it is clearer:
Methods without parameters or return values:

Function f1 () {alert ('this is a method');} f1 (); // call a method

Methods for returning values without parameters:

Function f2 () {return 100;} var result = f2 (); // declare a variable and receive the returned value alert (result) in f1 (); // 100

Methods With parameters and return values:

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 (); // After "this is a method" is displayed, undefined

Cause: In js, the variable is not assigned a value, that is, undefined; In this chestnut, f1 () does not return a value, so it is an unknown value (undefined), where the unknown variable is placed in alert (), of course, the pop-up is undefined.

NOTE 2:

Alert (f1); // if no parentheses are specified, the entire f1 code is displayed as a string: function f1 () {alert ('this is a method ');}

No method overload in JavaScript

Only call the latest defined method:

function f1(n1,n2){ alert(n1+n2); } function f1(n1,n2){ alert(n1-n2); } f1(10,2);//8

Conclusion: only the latest defined method is called wherever it is called.

Note: Number + undefined = undefined

Function f1 (n1, n2, n3) {alert (n1-n2 + n3);} f1 (); // NaN, because the n3 value is not passed, n3 is undefined, // Add undefined or undefined to the number

Conclusion: There is no method to overload Javascript.

Note the following when defining a method:

The custom function name must not be the same as that of the built-in method:
Do not use the same name as the built-in js and dom built-in methods, such as selectAll and focus functions.

Do not duplicate the name with the system function. (The custom focus method is called in the click event. It is the same as the focus () method of the system)

Note:

Generally, braces written in js are directly followed.

Function f1 () {return {age: 100 };} var s = f1 (); alert (s. age); // undefined. The s result is undefined, and undefined. age must be undefined.

Anonymous method (many are used)

Why is the anonymous method recommended?

1. There is a method in 1.js with function aa () {alert {'I'm pretty handsome '}}

2. There is a method in 2.js that function aa () {alert {'I'm getting handsome '}}

3366import 1.js and 2.jsto index.html and Call aa (). The result shows that I am getting more and more handsome.

Conclusion: The aa () method in 2. js will overwrite aa () in 1. js ()

What should I do? No longer specifies the method name. Use the anonymous method.

First, 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

Anonymous Method for writing a row:

The Code is as follows:

(Function (n1, n2) {alert (n1 + n2) ;}) (9, 9 );

Small case: 1:

Var x = 1; var y = 0; var z = 0; var add = function (n) {n = n + 1; return n}; y = add (x ); // The result is 2. First Call add = function (n) {n = n + 3; return n ;}; z = add (x ); // The result is 4. Call the add alert (y + ',' + z) method. // 2, 4

Case 2:

Function aa () {alert ("aaa"); return function () {alert ("bbb") ;}}alert (aa); // No parentheses are written, the entire code of the aa method is displayed as alert (aa (); // aaa, function () {alert ("bbb") ;}; aaa is not explained, the following string is used as the return value of aa () to show alert (aa (); // aaa, bbb, undefined // The following statement is used to describe var s = aa (); // aaa alert (s (); // s () is function () {alert ("bbb") ;}; the bbb is displayed first, and the method does not return a value. Therefore, the undefined is displayed.

I hope this article will help you design javascript programs.

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.