In JavaScript, the most common method to define a function is to call a function statement. This statement is composed of function keywords and is also important for function reuse. 1. Define and call Functions
In JavaScript, the most common method to define a function is to call a function statement. This statement is composed of the function keyword, followed:
Ø function name
A list of parameters in parentheses. The parameters are optional and separated by commas.
Ø function body included in braces
Notes for using functions:
Ø if a return statement exists in the function body, it returns a value; if not, it returns undefined
Ø JavaScript does not check whether the number of function parameters is correct. If more real parameters are passed than the actual parameters, the excess parameters are not ignored. If less than the actual parameters are passed, the parameter not passed is assigned to undefined, in this case, the program runs incorrectly.
Script // print1 returns a value function print1 () {return 'ddxkj ';} // No return Statement for print1, returns the undefined function print2 () {} function print3 (str1, str2, str3) {document. write (str1, "<br>"); document. write (str2, "<br>"); document. write (str3, "<br>");} document. write (print1 (), "<br>"); document. write (print2 (), "<br>"); print3 ('aaa', 'bbb ', 'ccc '); // The number of real parameters and form parameters is the same as that of print3 ('aaa', 'bbb ', 'ccc', 'ddd ', 'Eee '); // The number of real parameters is greater than that of the image parameter print3 ('aaa', 'bbb '); // The number of real parameters is smaller than that of the image parameter script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
Running result:
Ddxkj
Undefined
Aaa
Ccc
Aaa
Ccc
Aaa
Undefined
1.1. nested Functions
JavaScript 1.2 and ECMAScript v3 allow a function definition to appear in another function, that is, function nesting. It should be noted that ECMAScript v3 does not allow function definitions to appear at will, and they are still restricted to the top-level global code and top-level function code, this means that function definitions cannot appear in loops and condition statements. For example:
Script function print (a) {function square (x) {return x * x;} document. write (square (a);} print (100); script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
Running result:
10000
1.2. Function () constructor
In ECMAScript v1 and JavaScript 1.1, a Function can be dynamically defined using Function () and new. For example: var f = new Function ("x", "y", "return x * y ;");
This line of code is almost equivalent to: function f (x, y) {return x * y ;}
The Function () constructor can accept any number of string parameters. The last parameter is the Function body, which can contain any JavsScrpt Statement, which is separated by each line of statements. If a function has no parameters, you only need a string of the function body.
A Function () constructor is also called an anonymous Function and does not define a Function name.
Function () constructor purpose:
Ø dynamically create and compile a function. Every time you call the functions Function, the Function () constructor must compile the functions, which becomes a disadvantage. Therefore, Function () is generally not used to construct a Function in a loop or frequently called Function.
Define a function as a JavaScript expression instead of a statement. However, in JavaScript, if you want to define a function in an expression instead of a statement, it is more convenient to use the function directly.
1.3. Direct function quantity
The syntax of a function's direct quantity is very similar to that of a function statement, except that it is used as an expression rather than a statement without specifying a function name. Like the function statement and Function () constructor, the direct quantity of functions is also one of the ways to create functions. For example:
Ø function f (x) {return x * x;} // function statement
Var f = new Funtion ("x", "return x * x;"); // Function () constructor
Ø var f = function (x) {return x * x ;};
Although the function is directly created with an unnamed function, you can also specify the function name.
Recursive functions are very useful. For example:
Var f = function fact (x) {if (x <= 1) return 1; else return x * fact (x-1 );};
The code above defines an unnamed function and stores its reference in f. It does not create the fact () function, but allows the function body to use this name without calling itself.
A function can be assigned, transferred to, or even called directly like a variable. For example:
Script var f1 = function (x) {return x * x;}; // define and save var f2 = function (x) {return x * x ;} (5); // directly call document. write (f1 (10), "<br>"); document. write (f2, "<br>"); script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
Running result:
100
25
2. functions as data
When a function is made as a data type, it can be assigned a value like a variable, stored in an object attribute or array, and passed to the function.
When defining a function as follows, function square (x) {return x * x;} defines a function object and assigns this function object to square. Square is meaningless here, but it is just an object that stores function objects. You can assign this function to other variables. For example:
Script function square (x) {return x * x;} var a = square (4); document. write (a, "<br>"); // print 16 var B = square; var c = B (10); document. write (c, "<br>"); // print the 100 script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
Running result:
16
100
Script function add (x, y) {return x + y;} function subtract (x, y) {return x-y;} function multiply (x, y) {return x * y;} function pide (x, y) {return x/y;} function operate (operator, operand1, operand2) {return operator (operand1, operand2 );} var I = operate (add, operate (add, 2, 3), operate (multiply, 4, 5); document. write (I, "<br>"); var operators = new Object (); operators ["add"] = function (x, y) {return x + y ;}; operators ["subtract"] = function (x, y) {return x-y;}; operators ["multiply"] = function (x, y) {return x * y;}; operators ["pide"] = function (x, y) {return x/y;}; operators ["pow"] = Math. pow; function operate2 (op_name, operand1, operand2) {if (operators [op_name] = null) return "unknown operator"; else return operators [op_name] (operand1, operand2);} var j = operate2 ("add", "hello", operate2 ("add", "", "world") var k = operate2 ("pow ", 10, 2) document. write (j, "<br>"); document. write (k, "<br>"); script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
Running result:
25
Hello world
100