Define a scope for javascript Function constructor

99封情书 提交于 2019-12-24 06:34:08

问题


I'm trying to create some dynamic functions that require an enclosing scope. Currently this works:

eval("with (scope) (function () {return scope.prop})")

This approach seems a bit hacky. User supplied code is nowhere near this so I'm not worried about security, it's just that it seems like there should be a better way.

I know the Function constructor does not include anything apart from the global scope, but I was hoping there was some way to 'inject' scope into the Function constructor. Not call or apply or bind, but construct a function with a defined scope.

Is it possible to define a scope when using the Function constructor? Is it something that may be included in the standard in the future?

来源:https://stackoverflow.com/questions/41475260/define-a-scope-for-javascript-function-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!