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

Scope Chains and Identifier Resolution

function add(num1, num2){
    var sum = num1 + num2;
    return sum;
}




Scope Chain Augmentation

with表达式会临时改变execution context中的Scope chain,一个新的包含指定对像所有属性的可变对象被插入到execution contextscopechain的最前端。这样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引用,所以还不能销毁。 因此使用闭包的函数比未使用闭包的函数需要更多的内存



访问idsaveDocument会有性能下降

 

Object Members


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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值