calling eval() in particular context

前端 未结 14 1038
说谎
说谎 2020-11-27 17:05

I have following javaScript \"class\":

A = (function() {
   a = function() { eval(...) };
   A.prototype.b = function(arg1, arg2) { /* do something... */};
}         


        
14条回答
  •  有刺的猬
    2020-11-27 18:08

    Edit

    Even though, eval.call and eval.apply do not force the context to be passed in correctly, you can use a closure to force eval to execute in the required context as mentioned in the answers of @Campbeln and @user3751385

    My original answer

    This is not possible. Eval is called only in the local context(is used directly) or in the global context (even if you use eval.call).

    For example, a = {}; eval.call(a, "console.log(this);"); //prints out window, not a

    For more information, look at this great article here

提交回复
热议问题