JavaScript seven notes

1. Function
- The return value is the result of the function execution.
- using a return to set the return value.
- Syntax: return value;
- the value will be the function's return value, the return value can be received by a variable
- return behind the code is not enforced, once return statement, the function will exit immediately.
- with the return value of any type may be a base data type may be an object.
- If the return value is not followed, or was not written by default return the function returns undefined.
- break, continue and return
- BREAK
- exit the loop
- Continue
- skip cycles when
- return
- Exit Function

- parameter argument function may be an arbitrary data type.

- method (Method,)
- a function that can be set to an object's property,
when the property of an object is a function,
we call this function is a method of that object.
- Object method name ();
- function name ();

2. Scope
- Scope scope is simply a variable.
- in JS Scope divided into two:
1. global scope
- code written directly in the script tag run in the global scope
- global scope is created when you open the page, destroyed when the page is closed.
- have a global scope global object window, window object is provided by the browser,
can be used directly in the page, it represents the entire browser window.
- in the global variable will be created in the domain as a window object's properties save
method in the global created in the domain will function as a window object is saved
- can be accessed anywhere in the page in the role of global variables and functions created in the domain.
In the scope function you can also access into the global scope of.
- Try not to create a global variable in the

2. Function scope
- the scope of function scope is created when the function is executed, each call to the function will create a new function scope.
- function scope is created when the function is executed, destroyed at the end of the function execution.
- you can not access the global variables in the function scope created.
- When using a variable in the function scope, it will first look for the domain in its own role,
If you find you directly, if it is not found on a scope to look for,
and if found use, can not find it, keep looking up, always will be

- to declare variables in advance
- in the global domain, using var keyword to declare the variable will be declared before all the code execution, but not the assignment.
So we can use the variable before the variable declaration. But do not use var keyword to declare variables will not be declared in advance.
- On the function scope, also has this feature, variable declared using the var keyword will be declared before the function of all of the code is executed,
if no var keyword to declare a variable, the variable will become global variables

- function declaration advance
- function (function fun () {}) in the use of global scope function declarations created, will be created before any code is executed,
that is, we can call the function in the function declaration to go, but the use of function expressions (var fun = function () { }) to create a function that is not characteristic
- in scope function, use the function to create function declaration, it was created before the code will be performing all the functions.

3.this (context object)
- every time we call a function, the parser will be a context object passed into the function as an implicit argument.
Used to refer to this context object, according to different function calls, this value is also different.
- this is a different situation:
1. When you call a function in the form of, this is the window
when the form 2. method calls, this is the object method of calling
object 3. Call the constructor of the form, this is a newly created

4. constructors
- the constructor function is designed to create an object
- a constructor that we can also be called a class
- object created by a constructor, we call this instance constructor when the object
- by the same structure function to create an object of a class of objects we call
- the constructor is an ordinary function, but he is called by different,
if called directly, it is an ordinary function
if you use new to call, it is a constructor

- example :
function the Person () {

}

- constructor function execution process:
1. Create a new object
2. the new object as a function of the context object (the this)
3. perform the function code
4. the new object is returned

- instanceof used to check whether an object is an instance of a class
- syntax: instanceof Object constructor
- If an instance when the object constructor returns true, otherwise returns false
- Object is the ancestor of all objects, so any objects and do instanceof Object returns to true

- the enumeration object attributes
for ... in
syntax:
for (var attribute name in the object) {

}

for loop ... experience in the statement is executed multiple times, the object has several properties that will be performed several times,
each time to speak the name of an attribute defined variables assigned to us, we can it to get object properties



 

 

 

 



Guess you like

Origin www.cnblogs.com/Jiang-jiang936098227/p/11595443.html