var obj={
name:"obj1"
}
var name='window';
function test(){
var name='scope';
with(obj){
console.log(name);//obj with能改变作用域链,能让作用域链的最顶端变成with括号里的
}
}
test();
//with的使用
var org={
dp1:{
jc:{
name:'abc',
age:123
},
deng:{
name:"deng",
age:34,
}
},
dp2:{
}
}
with(org.dp1.jc){
console.log(name);
}
with(org.dp1.deng){
console.log(name);
}
//再比如,document.write等系列用到document的方法,简化也可以用到with
//可以写成
with(document){
write('a');//上document的执行期上下文去找write
}
</script>