javascript eval in context without using this keyword

后端 未结 4 1615
逝去的感伤
逝去的感伤 2021-01-14 00:54

I am trying to execute eval within a particular context. I have found the answer here useful. However I am getting the following behavior in Chrome Version 53.0.2785.143 m.

4条回答
  •  半阙折子戏
    2021-01-14 01:00

    whilst I recommend the answer provided by @trincot and in particular the great link. I am posting here my solution to the problem I faced

    function evalInContext(scr, context)
    {
        // execute script in private context
        return (new Function( "with(this) { return " + scr + "}")).call(context);
    }
    

    The with(this) expression allows the member variables of the context object to be present in the execution scope of the expression scr.

    Credit to this answer to a similar question

提交回复
热议问题