Some points for attention of JavaScript function and _javascript techniques of JS anonymous function

Source: Internet
Author: User
Tags anonymous

Here's a description of JavaScript functions

The basic syntax for a function is:

function functionname (arg0,arg1,..., argn) {
 statements
}

Here's an example:

function str (name,age) {
 document.write ("Hello my Name" + name +). And I am "+ age +" years old. ");
STR ("Oliver", 23); Hello my name is Oliver. And I am years old.

In addition, any function can implement the return value at any time by following the return statement followed by the value to be returned. Such as:

function sum (num1,num2) {return
 num1 + num2;
 Alert ("Hello"); Do not proceed with alert
} var result
= SUM (321,32)
after returning return document.write (result); 353

Any code that follows the return statement will not be executed because the return statement is stopped and exited immediately after execution.

Of course, a function can contain multiple return statements. Such as:

function Conp (a,b) {
 if (a > B) {return
 A;
 } else if (a = = b) {return
 "equal";
 } else{return
 b;
 }
var result = Conp (4,4);
document.write (result); Equal
var result = Conp (321,4);
document.write (result); 321

Alternatively, the return statement can have no returned value. This way, you can stop the function execution immediately and return to undefined. Such as:

function Conp (a,b) {
 if (a > B) {return
 ;
 document.write ("bad");
 } else{
 document.write (b);
 }
var a = Conp (33,3);
document.write (a); Return to undefined and no "bad" appears

Parameters of the function

The ECMAScript function can be any number of arguments, or any data type. It can be accessed through the arguments object in the function body, such as the first parameter is arguments[0], the second is arguments[1], and so on. The named parameters are only convenient, but not necessary. Such as:

function greeting () {
 document.write ("Hello" + arguments[0] + ". Look "+ arguments[1] +". ");
Greeting ("Oliver", "good"); Hello Oliver. Look good.

In addition, you can obtain the number of arguments passed to a function by accessing the length property of the arguments object. Such as:

function countarguments () {
 document.write ("There are" + arguments.length + "arguments here");
}
Countarguments (321,321,32,32); There are 4 arguments here.

You can use this to make judgments with if statements. Such as:

function count () {
 if (arguments.length = 1) {
 document.write ("You just have 1 arguments.");
 } else{
 document.write ("You have many arguments.");
 }
Count (321,321,321)//you have many arguments.

In addition, arguments[] can be used with named parameters.

Overloads of Functions (no overloads)

If you define two parameters with the same name, the name is changed to only the later defined function. Such as:

function Add () {
 document.write (Arguments[0] + arguments[1]);
}
function Add () {
 document.write (arguments[0] +);
}
Add (321,2); 421 does not perform the first function (adding two arguments), only the last function with the same name (the first parameter plus 100)

Ps:javascript anonymous function

A function is one of the most flexible objects in JavaScript, and it simply explains the purpose of its anonymous function. anonymous function: A function that has no function name.

1.1 The definition of function, first of all, a simple introduction to the definition of function, roughly can be divided into three ways

The first: This is also the most conventional one

function double (x) {return
 2 * x; 
}

The second: This method uses the function constructor, the argument list and function body as a string, very inconvenient, not recommended.

Copy Code code as follows:

var double = new Function (' x ', ' return 2 * x; ');

The third type:

Copy Code code as follows:

var double = function (x) {return 2* x;}

Notice that the function to the right of "=" is an anonymous function that creates the function and assigns it to the variable square.

1.2 Creation of anonymous functions

The first way: this is the definition of the square function, which is one of the most common ways.

The second way:

(function (x, y) {
 alert (x + y); 
}) (2, 3);

An anonymous function (within the first parenthesis) is created here, and the second parenthesis is used to invoke the anonymous function and pass in the argument.

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.