obj 与 prototype
对象包含属性和方法,当在对象中没有找到相关的属性时,会根据__proto __去查找他的prototype对象,如果存在,则返回;否则,返回undefined。就好像:
var a = { x : 100 }
var b = { __proto__ : a }
a.x; // 100
__proto __与prototype
__proto__ 会指向创建此对象的构造函数的prototype对象。这句话听起来可能很绕,大体的意思是js中所有东西都有自己的prototype,__proto__都会有所指,只是有时候没有找到那个你认为的那个。eg:function的__proto__指向Object.prototype
variable object
>A variable object is a container of data related with the execution context. It’s a special object associated with the context and which stores variables and function declarations are being defined within the context.
FE 如 (function baz(){})中baz不被包含在variable object中
scope chain
> if a variable is not found in the own scope (in the own variable/activation object), its lookup proceeds in the parent’s variable object, and so on.
从函数里面能够访问外面的作用域叫做scope chain,而从外面访问到里面的变量叫做闭包,而闭包也是解决作用域问题的方法。
常见的闭包的两种使用场景:
1.
function foo() {
var x = 10;
return function bar() {
console.log(x);
};
}
var returnedFunction = foo();
returnedFunction(); // 10, but not 20
2.
var x = 10;
function foo() {
console.log(x);
}
(function (funArg) {
var x = 20;
funArg(); // 10, but not 20
})(foo); // pass "down" foo as a "funarg"
> A closure is a combination of a code block (in ECMAScript this is a function) and statically/lexically saved all parent scopes. Thus, via these saved scopes a function may easily refer free variables. by Dmitry Soshnikov