JS -变量、作用域、内存

Two types of values can be stored in javascript variables: primitive values and reference values. Primitive values have one of the five primitive data types: Undefined, Null, Boolean, Number, and String. Primitive and reference values have the following characteristics:

  1. Primitive values are of a fixed size and so are stored in memory on the stack.
  2. Copying primitive values from one variable to another creates a second copy of the value.
  3. Reference values are objects and are stored in memory on the heap.
  4. A variable containing a reference value actually contains just a pointer to the object, not the object itself.
  5. Copying a reference value to another variable copies just the pointer, so both variables end up referencing the same object.
  6. The typeof operator determines a value’s primitive type, whereas the instanceof operator is used to determine the reference type of a value.

All variables, primitive and reference, exist within an execution context (also called a scope) that determines the lifetime of the variable and which parts of the code can access it. Execution context can be summarized as follows:

  1. Execution contexts exist globally ( called the global context) and within functions.
  2. Each time a new execution context is entered, it creates a scope chain to search for variables and function.
  3. Contexts that are local to a function have access not only to variables in that scope but also to variables in any containing contexts and the global context.
  4. The global context has access only to variables and functions in the global context and cannot directly access any data inside local contexts.
  5. The execution context of variables helps to determine when memory will be freed.

Javascript is a garbage-collected programming environment where the developer need not be concerned with memory allocation or reclamation. Javascripts’s garbage-collection routine can be summarized as follows:

  1. Values that go out of scope will automatically be marked for reclamation and will be delated during the garbage-collection process.
  2. The predominant garbage-collection algorithm is called mark-and-sweep, which marks values that aren’t currently being used and then goes back to reclaim that memory.
  3. Another algorithm is reference couting, which keeps track of how many references there are to a particular value. Javascript engines no longer use this algorithm, but it still affects Internet Explorer because of nonnative Javascript objects (such as DOM elements) being accessed in Javascript.
  4. Reference counting causes problems when circular reference exist in code.
  5. Dereferencing variables helps not only with circular references but also with garbage collection in general. To aid in memory reclamation, global objects, properties on global objects, and circular references should all be dereferenced when no longer needed.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值