Home > front end >  JavaScript function summary
JavaScript function summary

Time:09-21

Function description:
Function function
- function is also an object
- the function can be encapsulated in some function (code), call when needed to use these functions (code)
- the function can save some of the code from the call when there is a need for
- use typeof detect when a function object, returns the function

A, create function
1, use the constructor to create a function object
Example: fun1=new Function ();
Fun1 ();//function call syntax: function object (); When calling a function, the function of encapsulation code will be in accordance with the order
2, the use of function declaration create a function object
Grammar: the function the function name ([form 1, form 2 refs... parameter n]) {
//statement
};
Description: [form 1, form 2 refs... parameter n] [] said here can use parameters, can not apply parameter
Call function: the function name ([argument 1, argument 2,... );
Example: the function add (a, b) {
C=a + b;
The console. The log (c);
};
Add (1, 2);
3, the use function to create a function/anonymous function
Example: the var fun3=function () {
//statement
};
Fun3 ();

Second, the parameters of the function
With a ", "between multiple parameters, statement is equivalent to the function parameters are declared within the corresponding variables, but not the assignment,
Example: the function add (a, b) {
C=a + b;
The console. The log (c);
};
Add (1, 2);
Note: 1, the function, the parser does not check the type of argument, so attention should be paid to whether might receive illegal parameters, if possible, the argument to check data types,
Example: the function add (a, b) {
C=a + b;
The console. The log (c);
};
Add (true, 123);
//results for true123 because pass in a true, add operation conducted will true implicit conversion to words
Character string, and then combined with 123, equivalent to the string links, so when possible to illegal parameters, need to check
Check the type of parameter,
2, when the calling function, the parser does not check the number of arguments, redundant real joining assignment, if the argument is less than the parameter,
There is no argument corresponding joining is undefined,
Example: the function sum (a, b, c) {
The console. The log (" a="+ a);
The console. The log (" b="+ b);
The console. The log (" c="+ c);
The console. The log (" sum="+ a + b + c);
};
The sum (1, 2);//argument is less than the parameter, the result: a=1, b=2, c=undefined, sum=NaN
The sum (1, 2, 3, 4);//argument redundant parameter, results: a=1, b=2, c=3, the sum=6

Three, the return value of a function with immediate execution function
1, the return value of a function: create a function to the sum of two Numbers, can return to return the function return value,
For example: the function add (a, b) {
C=a + b;
Return the c;
};
Var num=add (1, 2);
The console. The log (num);//the result: 3
2, immediately perform functions: function definition, are called immediately, this function is called the immediate execution function, immediate execution function is often just
Will only perform a,
For example: the function () {
Alert (" executive function immediately!" );
}) ();

Four, the function and the method
1, function: through the function name calls, call the function
For example: the function Hello () {
Alert (" Hello ");
};
Hello ();//by the function name calls, call function
2, methods: attributes of the object function can also be used as a, if a function as an object of property preservation, so we call this letter
For this object, the method of this function is called, is called the method of the object, function and the method is just the name on the area
Don't, don't, there is no other
For example: var obj={
Hello: Hello ();
}
Obj. Hello ();//as an object of method calls, call methods

Five, the statement in advance
1, the variable declaration in advance
Variables, use the var keyword statement may be declared before all the code execution (not assignment), if a declared variable need not shut the var
Key statement, the variable declaration does not advance,
Example: the console log (a);
var a=1;
//returns the undefined, because a is a statement in advance (assignment), so when the code execution to the console. The log
(a) returns undefined (a not assignment)
console.log(b);
B=2;
//error, b no statement in advance, and there is no assignment, so when the code execution to the console. The log (b) complains when

2, the function declaration in advance
Use the form of function declaration to create function function () {}, it can be carried in all of the code before they can be created, so can the letter
Several statement before calling function, using the function expression to create function will not be declared in advance,
Example: create functions in the form of function declaration of use:
//fun1 ();//can be invoked before the statement
The function fun1 () {
//statement
};
Fun1 ();//can also call after declaration

Using the function expression create functions:
Var fun2=function () {
//statement
}
Fun2 ();//can only call after function declarations, if before the function declaration call complains

Six, scope
Scope: scope refers to the scope of a variable
JS, a total of two kinds of scope: 1, 2, global scope function scope (local scope)
1, the global scope
- write directly in the JS files or scripts to JS code, are all in the global scope
- global role in created when the page is turned on, the page is closed to destroy
- the global scope has a window object, it represents a browser window, founded by the browser, can directly use the
- global role in: create variables are attributes of the object as a widow save
Global functions in the will of the window as the object of method save
- variables are global variables, in the global scope in any part of the page can access to
2, the function scope (local scope)
- create a function call a function in scope, function performs to end, destruction scope
- each call a function will create a new function scope, they were independent of each other between
- access to global variables in the function scope, but don't have access to a local variable in a global scope
- when the function scope of a variable operation, it will first search in its scope, if use directly, if you don't have to
At the next higher level search scope, if the global scope there is still no, may be an error ReferenceError
- you want to access global variables can be used in the function window object, because global scope to create the variables are as
Widow object's properties save

Six, this
This: parser every time when calling a function to the internal transfer an implicit function parameter, this parameter is this, this refers to the
To is an object, the object we call function performs the context object, according to the different methods of function calls, this
Can point to different objects (in the form of a function call, this is the window all the time, in the form of a method call, this
Is the object) method is called
Example: the function fun () {
Alert (1);
};
Fun ();//in the form of a function call, this is the window
Var obj={
Fun, fun
};
Obj. Fun ();//in the form of method calls, this is the obj
This conclusion:
1, in the form of a function call, this are always window
2, in the form of method calls, this is called the method that object (below)
3, in the form of the constructor invoked, this is the new object (below)
4, the use of the call and the apply call, this is to specify the object (below)

Seven, the constructor
Constructor: create a constructor to create a class object, the constructor is a normal function, creating way,
Function there is no difference, the difference is the constructor is used to capitalize the first letter, ordinary function call directly, the constructor need
Use the new key tone with
The constructor execution flow:
1, create a new object
2, set the newly created object to the function of this, in the constructor you can use this to refer to the new object
3, line by line to perform function code in the
4, will be the new object as a return value return
Use the same constructor creates objects, we referred to as a class object, will also be a class constructor is called, will use a constructor
Objects created, called an instance of this class,
Example: the function Person (name, age, address) {//Person class
this.name=name;
this.age=age;
This. The address=address;
};//in the form of the constructor calls this, this is the new object
Var p=new Person (" zhang ", 23, "guangzhou");//call
The console. The log (p);//the Person class instance
Using the instanceof check whether an object is an instance of this class, grammar: object instanceof constructor, if is
Returns true, if not, return false
Example: the console log (p instanceof Person);//true

Eight, the prototype object
1, we create each function, the parser will add an attribute to the function prototype, an object corresponding to the attribute
, this object is what we call a prototype object, if the function prototype as an ordinary function call has no effect, when the function
In the form of the constructor invoked, its objects will have created a hidden attribute points to the constructor of the prototype object, we
Can access the properties by __proto__
2, a prototype object is equivalent to a public area, all instances of the same class can access to the prototype object, we can
Objects of the communist party of China some content, unified set to the prototype object
3, a prototype object and object, so it also has a prototype, but we use an object property or method, will be in their search for
If itself is used directly, if not will go to the prototype object to find, if there is one on the prototype object is used, if there is no
To the prototype object of prototype object to find, until we find the prototype of the object, the object's prototype without prototype
, if still is not found in the object, it returns undefined
4, use in check for specifying the properties object, itself will return true, without their own prototype objects have also returns true
Using the object of the hasOwnProperty () method to check whether or not containing the property in the object itself, only when the object itself contains the
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related