如何在javascript中执行intheContext,以便更改范围

How to do intheContextof in javascript so that scope could be changed

本文关键字:范围 intheContext javascript 执行      更新时间:2023-09-26

b/c我使用的是angular js,我经常在查看$rootScope.something 时遇到解析难题

有可能做吗

 function inthecontextof(obj, func){
     func.call(obj);
 } 

在那里我可以做

inthecontextof($rootScope, function(){
    flamingo = 5 //$rootScope.flamingo is what im refering to
    $apply()
}

你必须这样写:

inthecontextof($rootScope, function(){
    this.flamingo = 5 //$rootScope.flamingo is what im refering to
    $apply()
}

现在我不确定你期望"$apply"在这里做什么;如果它要隐式引用"$rootScope",那么应该没问题。