Five function types
Each function is an instance of the type. Functions are also objects.
Declaring functions:
The function Func_name () {} //javascript parser will be the first to read the functions declaration when the program executes, making it available (accessible) var func_name = function () {} before executing any code; The parser executes to the line of code where it is located before it is interpreted.
So the function name is a variable that holds a pointer to a function object, not bound to a function, and is no different from the other variables that contain the pointer to the object.
1. No overloads
Declare two functions with the same name, and the second will overwrite the first one
2. Functions as values
The function name itself is a variable. The function body can therefore be used as a value. It can be passed as a parameter, or it can be used as a return value for other functions.
function Callsomefunction (someFunction, someargument) { return someFunction (someargument); }
3. Function Internal Properties
There are two special objects inside the function: arguments and this
The *arguments object holds the parameters inside all incoming functions.
*arguments.callee. This is a pointer to the function that owns the arguments object
function factorial (num) { if (num <=1) { return 1; } else{ return Num*arguments.callee (num-1)///Recursive call //equivalent: //return num*factorial (num-1); This eliminates the tight coupling between the function name and the internal recursive call function name }}
*this refers to the variable object of the environment in which the function is executed.
*caller Object Properties. This property holds a reference to the function that invokes the current object. If the current function is called in the global scope, the value is null
function outer () { inner (); } function inner () { alert (arguments.callee.caller);} Outer ();//Returns the source code of the outer () function
4. Function properties and methods
Length---The number of functionobject.length function arguments. function has no arguments, it returns 0
Apply ()---function.apply (scope[, function.arguments| | Array])//The parameter passed can be a arguments object, or it can be a parameter array
Call ()---function.call (scope[, Argument1,argument2, ...) The parameters to be passed must be listed as a blow.
Both of these functions are the scope in which to extend the runtime itself. Parameter 1, the scope in which the two functions run (typically objects), and parameter 2, which is passed to the parameter of the function.
Window.color = ' red '; var o = {' Color ': ' Blue '};function saycolor () { alert (color); } Saycolor (); Redsaycolor.call (this)//redsaycolor.call (window)//redsaycolor.call (o)//blue
The advantage of using these two functions is to reduce the coupling between the object and the method.
JavaScript Learning Notes-Reference type Function