Function:
Named function
anonymous functions
A. function Definition
(1) function declaration
A function declaration can only appear in a program or function body.
function Fun () {
function Body
}
(2) function Expression
An anonymous function expression
var fun = function () {
function Body
}
named function expression
var fun = function f () {
function Body
}
The function name of the named function expression can only be used inside the creation function
(3) function Constructors
Var fun = new Function (), generally not recommended
Two Function Call Method:
(1) called as a function, is triggered by the addition of a () operator after the function , when called in this way, the context of the function is the global context, that is, window Object
(2) method invocation Pattern: A function is a property of an object, and when the function is called, the function is considered a method of the object. When the function is called as a method of an object, the object becomes the context of the function.
The creation of the context of a function is not determined by the definition of the function, but by the invocation of the function.
(3) constructor call: You want a function to be called as a constructor, you need the new keyword, This is the newly created object
About the new process
1) Create an empty object that contains only The properties and methods of object.
2) Create a reference to the property and method in the prototype and assign the value to the new object
3) Create a new copy of the properties and methods on this and assign to the new object
4) returns The This object, ignoring the return statement.
(4)apply/call invocation: By using the apply and call methods, You can use any object as a function context and take its dominant action
Apply (), receive two parameters, the first parameter as the object of the function context, the second argument, an array of arguments
Call () is another simple form, the first parameter is the object of the function context, and the second parameter simply passes the parameter list instead of the parameter array.
three. Properties and methods of functions
Properties ofFunction ():prototype
Function Object Instance properties:
(1) The Arguments:arguments object is a class array object that contains all the arguments passed to the function, andthearguments object has a name of callee property, which points to the currently executing function, which can be used to get the function itself inside the function. the arguments object has the length property, which gives the number of arguments that are sent to the function at the time of invocation. This attribute has been abolished in js1.4.
(2) constructor
(3) Length: The Length property ofThis function instance provides the number of defined parameters that can be used to obtain the total number of parameters required by the function.
Function Object instance method:
(1) Apply ()
(2) Call ()
(3) toString ()
Four. anonymous functions , self-invoking anonymous function expressions, self-invoking anonymous function statements
Five. functions can be nested within other functions indefinitely
The function statement can be called at execution time before the function statement is actually defined, because the function declares the effect of the promotion. Using a function declaration statement , the function name and function body are all in advance, using the function definition expression ,JS will split this code into two lines of code executed separately. First, the variable name is lifted, and the expression of the function is still in its original position .
This article is from the "dream need to adhere to" blog, please be sure to keep this source http://xiyin001.blog.51cto.com/9831864/1783366
JS in function Learning notes