3. JavaScript Functions

Source: Internet
Author: User

3. JavaScript Functions
Knowledge Point Function Definition function call passing Parameter Function return value variable scope anonymous function callback function Self-Tuning Function embedded function recursion call Closure Function Definition

Let's look at the following code.

function print(){    console.log('123');}

The code above defines a function
-FunctionDefine function keywords
-PrintFunction Name
-{}The content in {} is the function body, that is, console. log ('20140901 ');

Function call
print();

The above code can call a function. The function call method isfuncName([arg1,arg2...,argN])

Passing Parameters

First, define a function with parameters.

function print(str){    console.log(str);}

In this function, str is a parameter and the call method isprint('123'). At this time'123'Passed instrYou can use str in the function.

Note
-The preceding function is used as an example. When you call a function, you do not need to input parameters. The syntax is correct. ThenstrIsundefined
-You can also input multiple parameters, suchprint('123','456'). Because only one parameter is defined during function definition, only the first parameter is received here.

Arguments array built in Javascript Functions
Each function has a built-in arguments array, which can return all parameters received by the function, regardless of whether the function has defined parameters.

function print(){    console.log(arguments);}//-------------> print('123','456')> ["123", "456"]

The arguments array contains two elements:'123','456'.

Function return value
function add(num1,num2){    return num1 + num2;}> var result = add(1,2)> console.log(result );> 3

Using arguments to transform Functions

function add(){    var sum = 0;    for(var i=0;i

After the function is transformed, we can pass any number of parameters, but the parameter type must be number. If it is another type, the final result can only beNaN.

> var result = add(1,2,4,5)> console.log(result );> 12
Variable Scope

Start with the internal variables in the JS file.
All variables defined except js {} are global variables. As follows:

//This is a javascript filevar std = {};

Global variables can be used anywhere, but changes to global variables in any place will be reflected in other reference variables.

Var std = {}; std = 0; console. log (std); // here we define a global variable named std and assign it to 0. Then use console. log to print std. Then the std value has changed to 0, instead {}

The parameter name of a function is also a variable for the function itself. This variable can be used anywhere in the function body, but is only valid in the function body.

Function print (str) {console. log (str);} // str is valid only in the function body. str variables cannot be used in vitro.

In{}The variables defined in{}Valid

if(x > 2){ var t = true; console.log(t);}console.log(t);>true>Uncaught ReferenceError: t is not defined
Anonymous and callback Functions

A function without a function name.

function(a){ print(a);}

Application scenarios of anonymous Functions
1. As a variable value

var print = function(str){ console.log(str);}print('123')> 123
Passed as a function parameter
function process(data,callback){ if(data.success){ callback(data.msg); }}var myData = { success: true, msg: 'hello'}process(myData,function(msg){ console.log(msg);});

Here

function(msg){ console.log(msg);}

It is an anonymous function and a callback function. A callback function is used to pass a function as a variable to another function.
The callback function is not necessarily an anonymous function. The following code can be modified.

function print(str){ console.log(str);}proccess(myData,print);
Self-tuning functions

A function can be called by itself after being defined.

(function(){ print('123');})()>123

Passing parameters of self-tuning functions

(function(msg){ print(msg);})('123456')
Embedded Functions

Define a function in the function. The scope of embedded functions is the function body.

Recursive call

A function calls itself in the function body, which is usually used to traverse Tree nodes.

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.