如何在一个范围内执行JS代码的不同部分 [英] How to execute different partsof the JS code in one scope

查看:155
本文介绍了如何在一个范围内执行JS代码的不同部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个相互依赖的脚本块。我需要在一个范围内执行它们。

I have several script blocks depend on each other. I need to perform them in one scope.

我的尝试:

var scopeWrapper = {}; 

with(scopeWrapper) {
    (function() {
        this.run = function(code) {
            eval(code);
        };
    }).call(scopeWrapper);
}

scopeWrapper.run('function test() { alert("passed"); }');
scopeWrapper.run('test();');

我得到'test not defined'错误。似乎代码在不同的范围内执行。
为什么会发生这种情况?

I get 'test is not defined' error. It seems that the code is executed in different scopes. Why is this happening?

推荐答案

编辑: Bergi 指出我原来的回答是错误的,他是对的。由于 eval 在其自己的作用域中运行,并且函数构造函数仍然根据 spec 这两种情况都不可能。

Bergi pointed out my original answer was wrong, he is correct. Since eval runs in its own scope and the function constructor still runs in function scope according to the spec this is not possible with either.

虽然我自己已经多次使用节点做过这种事情。 js使用 vm 模块,您可以更好地控制代码执行的位置,似乎浏览器需要采用不同的方法。

While I have done this sort of thing myself several times with node.js using the vm module where you get much finer grain of control over where your code executes, it seems browsers require a different approach.

以这种方式共享变量的唯一方法是在JavaScript执行的全局范围内(可能在iframe中)这样做。一种方法是脚本标记注入。

The only way you can share variables in such a way is to do so in the global scope of JavaScript execution (possibly, in an iframe). One way you could do this is script tag injection.

function run(code){
    var sc = document.createElement("script");
    sc.setAttribute("type","text/javascript");
    sc.innerHTML = code;
    document.body.appendChild(sc);
}

run("var x = 5");
run("document.write(x)");

这里有代码在运行

对于范围包装器,不是将它们注入同一帧中而是将它们注入另一个的iframe 。这会将他们的窗口对象范围限定为iframe,并允许您共享上下文。

As for the scope wrapper, instead of injecting them in the same frame inject them in another iframe. That will scope their window object to that iframe and will allow you to share context.

我谦虚地为我之前的回答道歉,我误读了规范。我希望这个答案可以帮到你。

I humbly apologize for my previous answer, I misread the spec. I hope this answer helps you.

我在这里留下我之前的答案,因为我仍然认为它提供了一些关于如何 eval 函数构造函数工作。

I'm leaving my previous answer here because I still believe it provides some insight into how eval and the Function constructor work.

当以非严格模式运行代码 eval 在页面的当前上下文中运行
完成函数声明后,声明的范围在die中声明,并且它的功能。

When running code in non-strict mode eval runs in the current context of your page After your function declaration is done, the scope it was declared in dies, and with it the function.

考虑使用函数构造函数然后 .call ing it

Consider using the Function constructor and then .calling it

在你的大小写如下:

var scopeWrapper = {}; 
scopeWrapper.run = function(code){
    var functionToRun = new Function(code);
    functionToRun.call(scopeWrapper);
}
scopeWrapper.run('this.test = function() { alert("passed"); }');
scopeWrapper.run("this.test()")

这是一个直接参考来自规范:


如果没有调用上下文或者eval代码没有被eval函数直接调用(15.1.2.1.1)评估,则
初始化执行上下文,就好像它一样是一个全局执行上下文,使用eval代码作为C,如10.4.1.1中所述。

If there is no calling context or if the eval code is not being evaluated by a direct call (15.1.2.1.1) to the eval function then, Initialize the execution context as if it was a global execution context using the eval code as C as described in 10.4.1.1.

如果此代码在节点中运行。 js考虑使用 vm 模块。另请注意,这种方法在允许您运行的代码更改代码的方式上仍然不安全。

If this code is run in the node.js consider using the vm module. Also note that this approach is still not secure in the way it'll allow code you run to change your code.

这篇关于如何在一个范围内执行JS代码的不同部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆