`

CHAPTER 5 Statements

 
阅读更多
5.5.4 for/in

for (variable in object)
statement

for(var i = 0; i < a.length; i++) // Assign array indexes to variable i
console.log(a[i]); // Print the value of each array element

for(var p in o) // Assign property names of o to variable p
console.log(o[p]); // Print the value of each property

5.7.1 with

with (object)
statement

in non-strict mode: avoid using it whenever possible

document.forms[0].address.value

with(document.forms[0]) {
// Access form elements directly here. For example:
name.value = "";
address.value = "";
email.value = "";
}

避免使用with语句,你可以使用下面的方法:
var f = document.forms[0];
f.name.value = "";
f.address.value = "";
f.email.value = "";

范围链只是用来查找标识的,而不是创建新的的,考虑下面的语句:
with(o) x = 1;
如果o有一个x属性,那么这个属性将赋值1.如果没有的话,那么不会给o创建一个x属性,x相当于在with语句的外面,可能会创建一个本地或者全局的属性。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics