javascript--function (i)

Source: Internet
Author: User

A function is a code block that aggregates code that implements a function so that it can be reused.

First, the creation and invocation of functions

The basic syntax declares functions and calls function obj () {   alert (1)   }obj ()//literal definition function (self-invocation of anonymous function) (function () {    //Function function code block})//object Form Declaration function <body><div onclick= "Fname3 ()" style= "background:red;width:100px;height:100px" ></div>   
<!--event call--! ></body><script> var fname3=new Function ("alert (1)");</script>

Note: Functions that are declared in the basic syntax are parsed into memory before the page is loaded, so they can be called in front of the function, which involves the pre-parsing order in JS, but a function named in the form of an argument will be assigned only after the execution of the function, so it can only be called after it has been executed.

Second, the parameters of the function

1. The function of the parameter: can dynamically change the value of the corresponding variable in the function body, so that the same function body to get the results.

Formal parameters: When defining a function, the variable defined within the function's parentheses is called the parameter, which is used to receive the argument's

Argument: The variable or value passed in parentheses is called an argument when the function is called, and is used to pass arguments

2. Explanation of parameters

1) Type of parameter

Can be any type of data

2) Number of parameters

The number of arguments and parameters is equal, and each corresponds

The argument is less than the formal parameter, no error, and the extra parameter value is automatically assigned undefined

argument is greater than formal parameter, no error, arguments object to get

3) Arguments

Each time a function is created, the function implicitly creates a arguments object that contains information about the actual incoming parameter

Arguments properties of an object

--length Number of arguments obtained

--callee getting a reference to the function itself

--access the specific value of the incoming parameter, arguments[subscript]

function AA (a,b,c,d) {        console.log (arguments)        console.log (arguments.length)        Console.log ((arguments[ 3]))        Console.log (Arguments.callee)} AA (1, "Qwq", 4)

4) parameters of up to 25

return value of function: Result of function operation

Give the function a return value through the return statement, stop and jump out of the current function

Return statement value:

1) Any function has a return value, the default return value is undefined

2) A function can have multiple return statements, but only one return value

3) External reference function internal values need to return function values

function Obj (A, b )    {var num=a+B;} console.log (obj (//undefined// external reference function internal value requires return value function obj (b) {    var num=a+b;     return num;} Console.log (obj (//3

Four, the function of the operating environment

1) scope, scope chain

Scope: A variable has meaning within a code snippet

Scope Chain: The function implicitly creates a collection at run time that holds variables and objects in the visible range of the function, which is the scope chain.

2) Operating Environment

Hosting Environment: Browser

Execution Environment: The execution environment determines the scope of variables and functions

A. Global Environment: Window

B. Local environment: internal function

3) Global variables, local variables

Improve the logic and security of the program (Var makes changes in the function's internal variables do not affect the value of the global variable, the value inside the function cannot be accessed) and the internal release (the function is completed, the garbage collection mechanism deletes the variables in memory)

Global variables: variable that can be accessed anywhere in the page, with global scope

Variables with the outermost definition of the function, variables with no direct assignment defined, have global properties

Local variables: can only be accessed in a fixed code fragment (function fragment)

A variable defined inside a function is a local variable, and a parameter is a local variable.

V. Pre-parsing of functions

JS Pre-parsing whether there are grammatical errors, punctuation errors, if there is enough space in memory to store variables and functions (what must be done)

1) The script block is parsed sequentially, from top to bottom into a piece, then the script block in the bottom can call the upper script block inside the function

2) The identifier (keyword) is parsed within each script block, and the Var declared variables and function are parsed to the beginning of the script block, the order of parsing is the order of writing, and the variable of VAR parsing is not assigned

3) inside the function, parse the variables and functions in turn to the beginning of the function.

//A variable declared in a function can be a global variable that is used at the time of the first callvarnub=200;functionFun () {varnub=300; Console.log (nub);}  Fun () Console.log (NUB); // $//Notice the effect of global variables on local variables in functionsvarnub=200; functionAA () {varnub=300; functionBB () {nub=400;  Console.log (NUB); //nub=400} BB (); Console.log (NUB)//nub=400} AA () Console.log (NUB); // the//variables can be overridden on a scope chain to assign values to local variablesvarnub=200; functionAA () {nub=300; functionBB () {nub=400;        Console.log (NUB); } BB ();//variable not declared directly assigned to global variablevarnub=200; functionAA () {varnub=300; functionBB () {nub2= 400;//variable not declared directly assigned to global variableConsole.log (NUB);//variables look up values on the scope chain} BB ();  Console.log (NUB)} AA () Console.log (NUB2); // the//pre-parse to parse variables at the beginning//First JS pre-parsing, var nub,var str,var arr parsing at the beginning//if satisfies the condition to be arr={}, is an empty object, and Var str is only declared, no assignment, so no error, for ubdefinedvarnub=200; if(nub<200){          varArr=[]    }Else{        varobj={}} console.log (Arr,obj)//undefined Object//executes without a conditional statementvarArr=[];varobj={};console.log (arr,obj)//Array object, empty object//Call to functionfunctionAA () {varnum=10; return function() {console.log (num++); }}        varFN=AA ()//A variable stores the function AA's return value functionFN ()//called is the return function return value of 10,num++ first output in the assignmentfn () fn () console.log (num)//error, num is local variable, external unreachable//10 11 12 Error//The pre-parsing of JS overwrites a variable or function with the same name, the variable only resolves the var declaration, no assignment, and the function parses all the beginning of the environment. Console.log (a)//function A () {Console.log (4)}    varA=1; Console.log (a)//1    functionA () {Console.log (2)} console.log (a); //1    varA=3;      Console.log (a); //3    functionA () {Console.log (4)} console.log (a)//3A ()//Error//function Internal Global variables are not parsed (no declaration of direct assignment)varA=1; functionFun (a) {Console.log (a)//1a=2; }fun (a); Console.log (a)//1

javascript--function (i)

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.