10-with语句

1
2
3
4
5
with(document.forms[0]){
name.value = "lee king";
address.value = "Peking";
zipcode.value = "10000";
}

===

1
2
3
document.forms[0].name.value = "lee king";
document.forms[0].address.value = "Peking";
document.forms[0].zipcode.value = "10000";

js的解释器需要检查with块中的变量是否属于with包含的对象,这将使with语句执行速度大大下降,并且导致js语句很难被优化。为了兼顾速度与代码量可以找到一个比较折衷的方案: