execution context 执行上下文

Each execution context has an associated variable object upon which all of its defined variables and functions exist.
浏览器里最外层的执行上下文的variable object是window,
每个函数也有自己的execution context

scope chain 作用域链

最开始的作用域链是当前正在执行的代码,最末尾的是global context
寻找variable总是从scope chain的开头往后找

一个函数里面的execution context是arguments和local variables
外面不能访问里面,里面可以访问外面
下面例子的scope chain里有3个context
swapColors -> changeColor -> global context

  1. var color = "blue";
  2. function changeColor() {
  3. let anotherColor = "red";
  4. function swapColors() {
  5. let tempColor = anotherColor;
  6. anotherColor = color;
  7. color = tempColor;
  8. // color, anotherColor, and tempColor are all accessible here
  9. }
  10. // color and anotherColor are accessible here, but not tempColor
  11. swapColors();
  12. }
  13. // only color is accessible here
  14. changeColor();

Object.freeze()

声明一个不能被改变的对象

  1. const o3 = Object.freeze({});
  2. o3.name = 'Jake';
  3. console.log(o3.name); // undefined