使用groovy,您可以运行关闭,如下所示:
instanceB.methodB({
methodA(3);
methodA(3);
});
//---- methodB definition
class B{
def methodB(Closure c){
c.delegate=new A(); //!!!---The question is about this "delegate" in JS
c.call();
}
}
您可能会注意到,我们会在没有methodA
(this
)的情况下直接调用this.methodA()
。
这是因为这条指令c.delegate=new A()
:因此,new A()
的所有方法都可以在那里调用。
如何使用箭头功能使用Javascript:ES6或ES7。
箭头功能是否具有类似delegate
的功能?
instanceB.methodB(()=>{
methodA(3);
methodA(4);
});
class B{
methodB(arrow){
arrow.delegate=new A(); // What's the right way, if any ?
arrow.call();
}
}
答案 0 :(得分:2)
不,箭头功能没有这样的功能,也没有任何其他功能。
范围在词汇表中是由词法决定的,即它取决于您声明函数的位置,并且您不能从外部注入任何内容或修改它。
您最好的选择可能是with
statement,但您必须将其放在您的(箭头)功能中,而不是在呼叫站点或eval
。