2016-07-28 72 views
0

使用Groovy,你可以像下面运行关闭不Arrow功能具有委职能属性如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内封闭无thisthis.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

有没有更新?没有人知道Groovy&JS –

+0

我不知道这与箭头函数有什么关系。 JS中没有特别的范围。 – Bergi

+0

你可能想看看http://stackoverflow.com/q/21562973/1048572 – Bergi

回答

2

没有,箭头功能不具有这样的功能,我也没有任何其他功能。

范围在JS词汇确定,即它取决于你在哪里声明你的职责,你不能注入任何东西,或从外部对其进行修改。

您最好的选择可能是with statement,但您必须将其放在您的(箭头)功能中,而不是在呼叫站点或eval

+0

可能由于@Bergi –