The "with" keyword in Javascript

The "with" keyword is used to specify an object to which all of the unreferenced properties and methods in a following block of code are to be applied.

for example:

Instead of coding

var x = document.body.scrollLeft;
document.write('text1');
document.write('text2');
document.write('text3');

you could use

with document {
var x = body.scrollLeft;
write('text1');
write('text2');
write('text3');
}

Of course using this construct results in code that may be somewhat shorter but it will also take a lot longer to run since each variable within the with block needs to be checked to see whether or not it is a property of the object that the with block references.

A more efficient coding that is almost as short is where you assign a much shorter name to reference the object and use that instead of the original object name. For example:

var d = document;
var x = d.body.scrollLeft;
d.write('text1');
d.write('text2');
d.write('text3');
No replies - reply
 

转载于:https://www.cnblogs.com/jinweijie/archive/2006/11/20/566610.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值