with

function fool (obj){
    with(obj){
        a=2;
    }
}
var o1={
    a:1
}
var o2={
    b:3
}
fool(o1)
fool(o2)
console.log(o1.a)//2
console.log(o2.a)//undefined
console.log(a)//2
console.log(b)//b in not defined

with 可以不需要重复引用的对象本身,with声明实际上是为你的对象凭空创建了一个新的作用域。

posted @ 2020-07-29 10:27  minfight  阅读(156)  评论(0)    收藏  举报