【发布时间】:2020-07-27 03:15:22
【问题描述】:
const obj1 = {
test: function() {
console.log(this);
}
};
const obj2 = {
test: () => {
console.log(this);
}
};
obj1.test();
obj2.test();
我的方法中需要一个新的作用域,但是在回调中运行某些东西后,我想将作用域绑定回全局对象,就像在obj2 中一样。
类似:
const obj1 = {
test: function() {
const newvalue = this.y;
scope = bind(scope)
this.globaldata = newvalue
}
};
我希望我的意思很清楚,我必须在回调中访问两个范围,实际上是 vue 实例中的 data 对象。有这样的可能吗?
【问题讨论】:
-
为什么不
const obj = { test(){ console.log(this); } };?见method notation。
标签: javascript data-binding this bind