我想存储一个表达式,并使用该范围内的变量在另一个作用域中计算该表达式。我不知道如何做到这一点。
我正在尝试使用回调来做到这一点。
class a {
constructor(b) {
this.r = b.a;
this.h = b.b;
}
check(_root) {
console.log(this.r);
if(this.r() == this.h) return true;
else return false
}
}
function callback() {
return _root.h;
}
c = new a({a: callback, b: 12});
console.log(c.check({h: 12}));
但是this.r()
抛出一个错误,指出_root未定义。
出于某种原因,我想避免在回调中传递_root
。
提前谢谢。
我不确定你想要什么,但也许你可以得到一些帮助。
class a {
constructor(b) {
this.r = b.a;
this.h = b.b;
}
check(_root) {
console.log(this.r);
if(this.r(_root) == this.h) return true;
else return false
}
}
function callback(_root) {
return _root.h;
}
c = new a({a: callback, b: 12});
console.log(c.check({h: 12}));