Scope Chains and Identifier Resolution
function add(num1, num2){
var sum = num1 + num2;
return sum;
}
![](https://img-my.csdn.net/uploads/201208/09/1344481904_8848.png)
Scope Chain Augmentation
with表达式会临时改变execution context中的Scope chain,一个新的包含指定对像所有属性的可变对象被插入到execution context的scopechain的最前端。这样local variable的访问就会变慢,所以不要使用with。
try {
methodThatMightCauseAnError();
} catch (ex){
alert(ex.message); //scope chain is augmented here
}
Dynamic Scopes
Both the with statement and the catch clause of a try-catch statement, as well as a function containing eval(), are all considered to be dynamic scopes.
Closures, Scope, and Memory
function assignEvents(){
var id = "xdi9592";
document.getElementById("save-btn").onclick = function(event){
saveDocument(id);
};
}
![](https://img-my.csdn.net/uploads/201208/09/1344482103_2378.png)
通常Activationobject会同execution context一起销毁,但是使用了closure后,Activation object仍然被closure引用,所以还不能销毁。 因此使用闭包的函数比未使用闭包的函数需要更多的内存
访问id和saveDocument会有性能下降
Object Members
![](https://img-my.csdn.net/uploads/201208/09/1344482219_6718.png)
function Book(title, publisher){
this.title = title;
this.publisher = publisher;
}
Book.prototype.sayTitle = function(){
alert(this.title);
};
![](https://img-my.csdn.net/uploads/201208/09/1344482257_3210.png)