JavaScript Functions and js anonymous functions, javascriptjs

Source: Internet
Author: User

JavaScript Functions and js anonymous functions, javascriptjs

The following describes javascript Functions.

The basic syntax of the function is:

function functionName(arg0,arg1,...,argN) { statements}

The following is an example:

function str(name,age){ document.write("hello my name is " + name + ". and i am " + age + " years old.");}str(" oliver",23); //hello my name is oliver. and i am 23 years old.

In addition, any function can return values at any time through the return statement followed by the value to be returned. For example:

Function sum (num1, num2) {return num1 + num2; alert ("hello"); // after return, alert} var result = sum (321,32); document. write (result); // 353.

Because the return statement is stopped and exited immediately after execution, no code after the return statement is executed.

Of course, a function can contain multiple return statements. For example:

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); //equalvar result = conp(321,4);document.write(result); //321

In addition, the return statement can return no value. In this way, you can immediately stop function execution and return undefined. For example:

Function conp (a, B) {if (a> B) {return; document. write ("bad");} else {document. write (B) ;}}var a = conp (33,3); document. write (a); // return undefined without "bad"

Function Parameters

The ECMAScript function can have any number of parameters or any data type. It can be accessed through the arguments object in the function body. For example, the first parameter is arguments [0], and the second parameter is arguments [1. Naming parameters are only convenient, but not necessary. For example:

function greeting(){ document.write("hello " + arguments[0] + ". you look " + arguments[1] + ".");}greeting("oliver","good"); //hello oliver. you look good.

In addition, you can access the length attribute of the arguments object to obtain the number of parameters passed to the function. For example:

function countArguments(){ document.write("there are " + arguments.length + " arguments here.");}countArguments(321,321,32,32); //there are 4 arguments here.

This can be used in combination with the if statement for judgment. For example:

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.

Function overload (no overload)

If two parameters with the same name are defined, the modified name only belongs to the post-defined function. For example:

Function add () {document. write (arguments [0] + arguments [1]);} function add () {document. write (arguments [0] + 100);} add (321,2); // 421 will not execute the first function (add two parameters ), only execute the last function with the same name (add 100 to the first parameter)

PS: JavaScript anonymous Functions

Functions are the most flexible objects in JavaScript. Here we will only explain the usage of anonymous functions. Anonymous function: A function without a function name.

1.1 define a function. First, let's briefly introduce the definition of a function, which can be roughly divided into three methods.

First: this is also the most common one.

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

Second: This method uses the Function constructor and uses the parameter list and Function bodies as strings. This method is inconvenient and is not recommended.

Copy codeThe Code is as follows:
Var double = new Function ('x', 'Return 2 * x ;');

Third:

Copy codeThe Code is as follows:
Var double = function (x) {return 2 * x ;}

Note that the function on the right of "=" is an anonymous function. After the function is created, the function is assigned to the variable square.

1.2 create an anonymous Function

The first method is to define the square function, which is also one of the most common methods.

Method 2:

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

An anonymous function (in the first bracket) is created here, and the second bracket is used to call the anonymous function and input parameters.

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.