First, the prototype chain (family genealogy)
- Concept: JS inside the object may have a parent object, the parent object will also have a parent object, ... Ancestors
Property: There is almost always a __proto__ property in the object that points to the meaning of his parent object: You can implement the object to access the related property in the parent object
- Root object: Object.prototype
var arr=[1,3,5]arr.__proto__:array.prototypearr.__proto__.__proto__ is finding the root object
function Animal () {}var cat=new Animal ();//cat.__proto__:animal.prototype//cat.__proto__.__proto__: Root Object
- Wrong understanding: In JS, everything inherits from object? --In JS, everything inherits from Object.prototype
Ii. scope (i), variable scope
- The concept of variable scope: a range that a variable can use
- JS First has an outermost scope: called the global scope
- JS can also be created by the function of a separate scope, where functions can be nested, so scopes can also be nested
var age=18; Age is a variable declared in the global scope: global variable function f1 () { console.log (name);//can access to the name variable var name= "Zhou Dong"//name is a variable declared inside the F1 function, So the scope of the name variable is console.log (name) inside the F1 function;//can access the name variable console.log (age);//age is declared in the global scope, so age can also access} Console.log (age); You can also access
-->1-level scope var gender= "male"; function fn () { console.log (age); Because age is a//age:undefined declared within the FN scope : Since there is a value that can access console.log (height),//height is not declared inside the scope, so it cannot be accessed //-- >2-level scope return function () { //-->3-level scope var height=180; } var age=5;} Note: the Declaration and assignment of a variable is the function fn () {Console.log (age) at two different times ; Undeinfed var age=18; Console.log (age); 18}//fn function execution, first find all the variables inside the function, function declarations, put them in scope, give the variable an initial value: undefined- -variable can access/execute code, in the process of executing code, if there is an assignment statement, Assign a variable to the function fn () { var age; Initial value: Undefined console.log (age); undeinfed age=18; Modified the value of the variable console.log (age); 18}
(ii), scope chain
- Because the scope is relative to the variable, and if there is a multilevel scope, where does this variable come from? This is a question that needs to be explored, and we call this variable's scope chain the process of finding a variable.
- Meaning of the scope chain: Find variables (determine where the variable comes from and whether the variable can be accessed)
- In a nutshell, a scope chain can be summed up in a few words: (or: Determine which scope a variable comes from)
- View the current scope and determine the result if the current scope declares the variable
- Find the ancestor scope of the current scope, which is the parent function of the current function, and see if there is any declaration in the parent function
- Then find the ancestor function of the parent function until the global scope
- If there is no global scope, we assume that the variable is undeclared (XXX is not defined)
function fn (callback) { var age=18; Callback ()}FN (function () { console.log (age); Analysis: Age Variable: //1, find current scope: no //2, find top-level scope: global scope //--> difficulty: Look at the first-level scope, not see where the function is called, but see where the function is written //-- > Because of this particular, we usually say the scope is: lexical scope})
Example 1:
var name= "Zhang San"; Function F1 () { var name= "abc"; Console.log (name); ABC } F1 ();
Example 2:
var name= "Zhang San"; Function F1 () { console.log (name); Underfind var name= "abc"; } F1 ();
Example 3:
var name= "Zhang San"; Function F1 () { return function () { console.log (name);//underfind } var name= "abc"; } var fn=f1 (); FN ();
Example 4:
var name= "Zhang San"; Function F1 () { return { say:function () { console.log (name); Underfind var name= "abc"; }} var fn=f1 (); Fn.say ()
JS Advanced (three)--prototype chain, closure, scope, function of four ways to call