The use of JS function

Source: Internet
Author: User

JS function application "Declaration and invocation of functions":1. function declaration:function Name (parameter 1, parameter 2, ) {//function Bodyreturn result;the format of the >> function call:function name (value of parameter 1, value of parameter 2, )Event Invocation   }2. A few highlights of the function declaration:①. The declaration of the function name must conform to the camel's rule (first letter lowercase, followed by the first letter of each word capitalized);②. Parameter list, you can have parameters without parameters, respectively, as a parameter function, no parameter function;③. The argument list when declaring a function is called the parameter list (the name of the variable); The calling function is a list of arguments called argument lists (the values of variables);The actual valid arguments in the function depend on the assignment of the argument, and the parameter that is not assigned will be undefined;④ function If the return value is required, return the result using return;when calling a function, accept the return result by using the var variable name = function name ();If the function does not return a value, the accepted result is undefined;Scope of the ⑤ function:In the function, the variable with var life, which defaults to the local variable of the function, functions only in the function;without var declaration, it is a global variable;the global variables in the function must be used after the function is called;The formal parameter list of the function, which is the local variable of the function, is used only inside the function;The ⑥ function declaration has no relationship with a function call, and the invocation statement can be written before the declaration. "Declaration and invocation of anonymous functions"1. Declare an anonymous function and assign it directly to an event;window.onload=function () {}2. Use function expressions to declare anonymous functions;declaring function Expressions: var func3=function () {}call Function expression: func3 ();>> using an anonymous function expression, the calling statement must be followed by an error after declaring the statement (comparing the general function declaration to the call) 3. Using self-executing function declarations (important): !function (Parameter 1) {} (value of parameter 1);//start with any operator, use it normally! (function () {} ());//use () to enclose the parentheses after the anonymous function level;( function () {}) ();//use () to wrap only anonymous function expressions;three ways of writing:1. Start adding! , End Plus (), not easy to mess, recommended use;2. Can indicate that the anonymous function and after the () as a whole, recommended to use;3. Unable to indicate function and after () as a whole, is not recommended to use; "Arguments Object"1. Function: The argument list for the assigned value when the function is saved. >> When we call a function and assign a value using an argument, the argument is actually saved to the arguments array, even if no formal parameter can be called using arguments[n]. the number of 2.arguments arrays depends on the actual parameter, regardless of the parameter, when the nth position of the formal parameter, the argument, the arguments are present, the shape participates in the arguements is synchronous (that is, in the function to modify one of the values, the other will change synchronously). 3.arguments.callee is an important property of argument for returning a reference to the function where arguements is located;Arguement.callee () can invoke its own function execution;calling the function itself within a function is called recursion, so Arguments.callee () is a common way of recursive invocation. "code execution order in JS" JS in the code to run, will first check, load, and declare variables, functions and other operations;and then the execution phase (the assignment of variables is the execution phase);the declaration of a function belongs to the check Mount stage, and the call to the function belongs to the execution phase, and the function declaration has no relationship with the function call, and the invocation statement can be written before the declaration. To illustrate: console.log (num); var num=10;Funn ();function FuncN () {}So, the above code, the execution process:-----Check the load stage-----var num; Variable declarationfunction FuncN ()//declaring functions-----Execution Phase-----console.log (num);num=10;Funn (); The {} Part of the execution function

The use of JS function

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.