此语句可以为一个或者一组语句指定默认对象,可以缩小代码量
with (object){
statements
}
(1).object:指定的默认对象。
(2).statements:一个或者一组语句,使用指定的object作为默认对象
l例子
var a=document.getElementById("x");
var b=document.getElementById("y");
document.write("大家好")
用with语句
with(document){
var a=getElementById("x");
var b=getElementById("y");
write("大家好")
}
二with语句能够暂时修改作用域链
with(document.forms[0]){
username.value="";
pw.value="";
phone.value="";
}
with语句会把document.forms[0]对象添加到作用域的顶端,当查找username、pw和phone这些标识符的时候就会在document.forms[0]对象中查找,一旦with语句执行完毕,作用域链恢复到原来状态。
var person={
a:"北京",
b:"欢迎您",
}
function fn(){
with(person){
a="美国";
c="日本";
}
}
fn()
console.log(person.a);//美国
console.log(person.c);//undefined
console.log(c);//日本