您的位置:首页 > Web前端 > JavaScript

<<High Performance JavaScript>>读书笔记-2.Data Access

2012-08-09 11:05 543 查看
Scope Chains and Identifier Resolution
function add(num1, num2){
var sum = num1 + num2;
return sum;
}








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);
};
}




通常Activationobject会同execution
context一起销毁,但是使用了closure后,Activation object仍然被closure引用,所以还不能销毁。
因此使用闭包的函数比未使用闭包的函数需要更多的内存



访问id和saveDocument会有性能下降

Object Members



function Book(title, publisher){
this.title = title;
this.publisher = publisher;
}
Book.prototype.sayTitle = function(){
alert(this.title);
};


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: