JavaScript with 初探

2020-09-23  本文已影响0人  ShoneSingLone

Vue 编译成render函数之后,会用with语法修改当前上下文。传入的是vm,当前实例。拥有实例方法与属性。故而在template中,可以使用this.某data或method或computed之类,也可以省略this。

with

(()=>{
    function f(vm) {
        with (vm) {
            console.log("with this.a", this.a);        
            console.log("with vm.a", vm.a)
            console.log("with a", a);
            console.log("with vm.b", vm.b)
            console.log("with b", b)
        }
    }

    f.call({
        a: 'callThis'
    }, {
        a: 'fnArgs'
    })
}
)()

....
with this.a callThis//通过call改变this指向
with vm.a fnArgs
with a fnArgs//with 改变上下文 a 没有声明,会从vm上查找
with vm.b undefined 
Uncaught ReferenceError: b is not defined//未声明
上一篇 下一篇

猜你喜欢

热点阅读