Function: A function is a block of code that can be called repeatedly. Arguments can be passed, and different parameters return different values. There are three ways to declare a function: 1. A code block that is declared by a function command function command. The function command is followed by a functional name, followed by a pair of parentheses (), which can be passed into the parameter. The function body is enclosed in curly braces. Function Show (name) {//code block ... console.log (name);} 2. function expressions in addition to declaring functions with a function command, you can also use variable assignment. var show = function (name) {//code block ... console.log (name);}; The notation is to assign an anonymous function to a variable. Because the assignment statement can only put an expression to the right of the equal sign, all this anonymous function is called a function expression. A function expression with a name that is valid in the body of a function and is not valid outside the function body. var show = function abc () {//code block ...//name function expression///The function will have a name attribute, the difference is that Show.name is ABC}; Note: The function appears as an expression, and the name is ignored. The function expression requires a semicolon at the end of the statement to indicate the end of the statement. 3. The function constructor function constructor can return exactly the same result without using the new command. This type of declaration is less used by people. var show = new Function ("X", "Y", "return x + y"); Equivalent to function show (x, y) {return x + y;} The last parameter of the function constructor is treated as the body of the functions, and if there is only one argument, that parameter is the body of the function. Duplicate declaration of a function: The same function is declared more than once, and subsequent declarations overwrite the previous declaration. Function Show () {Console.log (' A ');} show (); A function Show () {Console.log (' B ');} show (); The above code, after declaring that the show function overrides the previous. At the pre-compilation stage, because the function name is promoted to the front, the previous declaration is not valid anywhere. Promotion of function name: The JavaScript engine sees the function name as the variable name, and all functions declared with function command are promoted to the head of the code. Call First Show (); After declaring that because of "variable promotion", the code can execute function show () {.} with the assignment statement to define the functions, call first, and then declare the error. Show (); var show = function () {}//typeerror:show is not a function equivalent to the following form Var show; Show (); Show = function () {}; Note: Both function commands and assignment statements are used to declare the same functions, and the last one is always defined by an assignment statement. var show = function () {Console.log ("A");}; Function Show () {Console.log ("B");} show (); The scope of the A function itself: the scope at which the function is executed, the scope at which it is defined, and not the scope at which it is called. The properties and methods of the function: A. Name Property function's Name property returns the function's FirstName show () {} show.name; Show variable assignment defines the function, the Name property returns the variable name. var show = function () {//anonymous function expression}; Show.name; Show var show = function abc () {//well-known functional expression} Show.name; ABC B. Length Property (number of function parameters) function show (A, B) {//a B is the function's formal parameter} show.length; 2 C. ToString () Method: Returns a string that is the source code of the function; Function Show () {//note hide ();} show.tostring (); Result: "function show () {//note hide ();}" parameter defined at function declaration, called formal parameter-parameter; The arguments passed in when the function is called, called the actual parameters-arguments: function test (A, B) {// The inside of the function is equivalent to declaring two variables//var A; var b; There is a list of arguments inside the function-[arguments] is an array of classes//Gets the length of the formal parameter test.length; The number of parameters and the argument list arguments one by one corresponding, if one side of the modification is changed, exception: when the actual parameter is less than the formal parameter, modify the parameter variable, the corresponding arguments is undefined//return; 1. Terminate (default is a return at the end of the logic); 2. The function returns a value of return 123; } parameter features: Indefinite parameter, more formal parameter than the actual parameter, internal use of the default is undefined. argument is more than formal parameter, it is ignored; function call method: 1. Direct Call function (this point to window) function init () {//code block ...} init (); Window.init (); 2. As a method call (this points to the current object) var MyObject = {value:100, init:function () {console.log (this.value);}} myobject.init (); 100 3. constructor, using the New keyword to create a new function object call, (this points to the constructor instance being bound to) var init = function (status) {this.status = status;} init.prototype.getSt ATUs = function () {return this.status;}; var test = new Init (' constructor '); Console.log (Test.getstatus ()); constructor function; This points to the test
[Record] a function in JavaScript