3. JavaScript functions

Source: Internet
Author: User

Knowledge points
    • function definition
    • Function call
    • Pass the reference
    • return value of the function
    • Scope of the variable
    • anonymous functions
    • callback function
    • Self-tuning function
    • inline functions
    • Recursive invocation
    • Closed Package
function definition

Look at the following code.

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

The above code defines a function
- keywords for function definition functions
- Print function number name
-The contents of {} {} are the function body, i.e. console.log (' 123 ');

Function call
print();

The function can be called using the code above, and the function is called by thefuncName([arg1,arg2...,argN])

Pass the reference

First, define a function with parameters

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

In this function, STR is the parameter and is invoked in the same way print(‘123‘) . This is the ‘123‘ time to pass str in and you can use STR in the function.

Note
-Take the above function as an example, in the call can not pass the parameter, syntax is not wrong. strthe value at this time is undefined
-You can also pass in multiple parameters, such as print(‘123‘,‘456‘) . Only one parameter is defined when the function is defined, so only the first parameter is received here.

JavaScript function built-in arguments array
Inside each function is a built-in arguments array that returns all parameters received by the function, regardless of whether the function has any parameters defined.

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

In this case, there are two elements in the arguments array, respectively ‘123‘,‘456‘ .

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

Using Arguments transformation function

function add(){    0;    fori=0;i<arguments.length;i++){        sum += arguments[i]    }    return sum;}

After the function has been transformed we can pass any number of arguments, but the parameter type must be the type of #, and if it is the other type the final result can only be NaN .

varresultadd(1,2,4,5)> console.log(result12
Scope of the variable

First, the internal variables of the JS file.
All variables defined outside of 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 one place will be reflected in other reference variables.

std = {};std0;console.log(std);//这里我们定义了一个名为std的全局变量,然后为其赋值为0。然后使用console.log打印std这时std的值已经变为了0,而不是{}

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

function print(str){    console.log(str);}//str仅在函数体中有效,在函数体外是无法使用str变量的

The {} variables defined in are only {} valid in

if2){    vartrue;    console.log(t);}console.log(t);>trueReferenceError: t is not defined
anonymous functions and callback functions

Refers to a function without a function name.

function(a){    print(a);}

Application Scenarios for anonymous functions
1. The value as a variable

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

Over here

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

is an anonymous function and is also a callback function. A callback function refers to passing a function as a variable to another function for its use.
The callback function is not necessarily an anonymous function, so you can change the code below.

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

Refers to functions that can be called by themselves after they are defined.

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

The parameter of self-tuning function

(function(msg){    print(msg);})(‘123456‘)
inline functions

The value defines the function in the function. The scope of the inline function is the function body

Recursive invocation

The function calls itself in the body of the function and is typically used to traverse the tree node.

3. JavaScript functions

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.