Comments on JS variables and scope

ECMAScript variables:

 

1. Basic type value (simple data segment)

 

2. Type reference value (the object may be made of values ​​over) → objects stored in memory

 

------

 

Dynamic properties: only give type reference value dynamically add new attributes for future use.

 

------

 

Copy the variable value:

 

Copy primitive values ​​→ create a new variable object on the value of the new variable → copy to (independently of each other)

 

Copy Copy → reference value stored in the variable object to a type value of a new variable allocation space (copied is a pointer to the same object, interaction)

 

------

 

The arguments:

 

ECMAScript functions for all the parameters are passed by value.

 

(Outside of the function to the parameter in the copy function.

 

 → ECMA function parameters can imagine local variables. )

 

Explain the reasons:

 

A statement:

 

Parameters are a function of local variables, the parameters passed to the function in the external value of internal and external non-interacting, replication is passed by reference address, so he House external influence each other.

 

Argument 2: The object angle (parameter is an object obj)

 

------

 

Object parameters passed by value → → reference external object and objects outside the object of the same object point in the object is only a heap memory, and is a global variable.

 

Do not be mistaken in the role of a local variable in local modify objects in the global scope (external) reflected in the results of → internal function of executing the function will be immediately destroyed.

 

------

 

Execution Environment (sometimes also referred to as "Environment") and Scope

 

Execution environment defines all variables and functions white Niang function or have access to other data determine their own behavior → each execution environment has an associated variable object → defined in the environment are stored in this object.

 

------

 

Each function has its own execution environment → execution flow when entering a function → function of the ambient environment will be pushed into a stack to perform its function after → → pop-up before returning control to the execution environment of
 
code in a single environment execution, it creates a variable object scope chain (scope chain)

 

Scope chain purposes: to ensure orderly access to all variables and functions have access to the execution environment.

 

The front of the scope chain, is always variable object code environment where the currently executing
------

 

Internal environment through the scope chain all of the external environment, the external environment can not access the internal environment (environmental contact is linear, orderly → up search scope chain to check variable and function names)

 

Function parameters are also treated as a variable

 

------

 

Extend the scope chain:

 

Some statements can be a temporary increase in front-end chain variable scope (variable removed after code execution)

 

1.try-catch catch block statements

 

2.with statement
------

 

No block-level scope Javas

ECMAScript variables:

1. Basic type value (simple data segment)

2. Type reference value (the object may be made of values ​​over) → objects stored in memory

------

Dynamic properties: only give type reference value dynamically add new attributes for future use.

------

Copy the variable value:

Copy primitive values ​​→ create a new variable object on the value of the new variable → copy to (independently of each other)

Copy Copy → reference value stored in the variable object to a type value of a new variable allocation space (copied is a pointer to the same object, interaction)

------

The arguments:

ECMAScript functions for all the parameters are passed by value.

(Outside of the function to the parameter in the copy function.

 → ECMA function parameters can imagine local variables. )

Explain the reasons:

A statement:

Parameters are a function of local variables, the parameters passed to the function in the external value of internal and external non-interacting, replication is passed by reference address, so he House external influence each other.

Argument 2: The object angle (parameter is an object obj)

------

Object parameters passed by value → → reference external object and objects outside the object of the same object point in the object is only a heap memory, and is a global variable.

Do not be mistaken in the role of a local variable in local modify objects in the global scope (external) reflected in the results of → internal function of executing the function will be immediately destroyed.

------

Execution Environment (sometimes also referred to as "Environment") and Scope

Execution environment defines all variables and functions white Niang function or have access to other data determine their own behavior → each execution environment has an associated variable object → defined in the environment are stored in this object.

------

每个函数都有自己的执行环境 → 当执行流进入一个函数时 → 函数环境会被推入一个环境栈中 → 在函数执行后将其弹出 → 把控制权返回给之前的执行环境
 
代码在一个环境执行中时,会创建变量对象的一个作用域链(scope chain)

作用域链用途:保证对执行环境有权访问的所有变量和函数的有序访问。

作用域链的前端,始终都是当前执行的代码所在环境的变量对象
------

内部环境可以通过作用域链所有的外部环境,但是外部环境不能访问内部环境(环境的联系是线性的,有次序的 → 向上搜索作用域链以查询变量和函数名)

函数参数也被当做变量来对待

------

延长作用域链:

有些语句可以在作用域的前端链临时增加一个变量(变量在代码执行后移除)

1.try-catch语句的catch块

2.with语句
------

Javas没有块级作用域

Guess you like

Origin www.cnblogs.com/xiewangfei123/p/12044117.html