Detailed analysis of JavaScript function definitions and javascript function definitions
Function
Key points:
A). functions are first-class citizens (importance) in javascript)
B). A function is an object.
C). The function defines an independent variable scope.
Definition Method
A) Name function:
Unless defined in another function, the name function is global.
// Global name function add (x, y) {return x + y;} console.info (add (100,200); // 300
B) Anonymous functions:
An anonymous function is usually assigned to a variable and called through the variable.
var func = function (x, y) { return x + y; } console.info(func(5, 2)); //7
Anonymous functions are applicable to the following "anonymous functions executed immediately:
Console.info (function (x, y) {return x + y;} (100,200) // call now );
C) The definition method affects the code execution result.
The name function can be used before being defined.
console.info(sum(10, 10)); function sum(num1, num2) { return num1 + num2; }
The anonymous function must be defined before use.
//console.info(sumFunc(10, 10)); //Uncaught TypeError: Property 'sumFunc' of object [object Object] is not a function var sumFunc = function (num1, num2) { return num1 + num2; }; console.info(sumFunc(10, 10));
Function return value:
Return Value is generated using return. If no return is returned, the function returns undefined.
Function func () {} console.info (func (); // undefined function func2 () {return; // NULL return statement} console.info (func2 (); // undefined
The pitfalls hidden in return:
var func = function (x, y) { var sum = x + y; return { value : sum } }
There is no problem with this writing: the Object {value: 10} returned by calling func (5, 5}
However:
var func = function (x, y) { var sum = x + y; return { value: sum }; } console.info(func(5,5)); //undefined
If return is followed by a carriage return,
Call func (5, 5) to display undefined
The editor adds a semicolon after return. However, in this case, there is no use.
Functions are objects:
Function add (x, y) {return x + y;} console.info (add (100,200); // 300 var other = add; // other and add reference the same function object console.info (other (300,400); // 700 console.info (typeof other); // function console.info (add = other ); // true
Nested defined functions:
Within a function, you can define another function.
function outerFunc(a, b) { function innerFunc(x) { return x * x; } return Math.sqrt(innerFunc(a) + innerFunc(b)); } console.info(outerFunc(3, 4)); //5
Access external variables:
Internal functions can access external variables and parameters.
var globalStr = 'globalStr'; function outerFunc2(argu) { var localVar = 100; function innerFunc2() { localVar++; console.info(argu + ":" + localVar + ":" + globalStr); } innerFunc2(); //hello:101:globalStr } outerFunc2("hello");
Function of the returned function:
Because the function is an object, it can be used as the return value.
function outerFunc(x) { var y = 100; return function innerFunc() { console.info(x + y); } } outerFunc(10)(); //110
The above is all the content of this article. I hope you will like it.