简体   繁体   English

eval调用函数调用定义函数的eval

[英]eval calls function which calls eval which defines function

I know the title sounds convoluted, but to keep things dynamic there is a purpose for this rest assured ;) 我知道这个标题听起来很复杂,但为了保持动态,这个目的是为了放心;)

Examples (note that these example codes are assumed to be within an outer eval) 示例(请注意,假设这些示例代码位于外部eval中)

//Ex1 this works
eval('function test (){}');
test();

//Ex2 this doesn't work (myfunction definition is written below)
myfunction();
test(); //I get an error

If I defined myfunction globally (outside of the outer eval) I get this error: object is not a function 如果我全局定义myfunction(在外部eval之外),我会收到此错误:object不是函数

If I defined myfunction within the outer eval I get this error: object is not a function 如果我在外部eval中定义myfunction,我会收到此错误:object不是函数

//myfunction definition
function myfunction () {eval('function test (){}');}

Question is: how do I expand the scope of a function definition to just outside of the function it was defined within? 问题是:如何将函数定义的范围扩展到其定义的函数之外? I know about making an eval global (see alternate myfunction below), but that seems like overkill, I just want to increase the scope to the outer eval is all. 我知道如何制作一个eval全局(请参阅下面的替代myfunction),但这看起来有点矫枉过正,我只想将范围扩大到外部eval就是全部。 Is this possible? 这可能吗?

Update: The examples only define one function to keep is simple, but I wish expand it so that myfunction defines many functions, and what functions it defines is dynamic depending on other factors. 更新:示例只定义了一个保持简单的函数,但我希望扩展它以便myfunction定义许多函数,并且它定义的函数取决于其他因素是动态的。 Also I wish to retain the function names as well as the definitions. 我还希望保留函数名称和定义。 I may end up just putting the contents of myfunction into the outer eval if I can't find a solution other than making eval call globally, then I have to copy over the contents to everyplace that uses it. 如果除了全局调用eval之外我找不到解决方案,我可能最终只将myfunction的内容放入外部eval中,然后我必须将内容复制到使用它的每个地方。

//making eval global works, but I had hoped to just upscope to the calling eval
function myfunction(){var globaleval=eval;globaleval('function test(){}');}

Below has been edited since the initial question: Maybe you could make a var in outer eval, have myfunction return the address of the function definition to that var. 从最初的问题开始编辑以下内容:也许你可以在外部eval中创建一个var,让myfunction将函数定义的地址返回给var。 However, I wish to retain the function names as well as the definitions. 但是,我希望保留函数名称和定义。

OK, so I am assuming you actually mean you want to control the scope in which eval uses... 好的,所以我假设你实际上是指你想要控制eval使用的范围......

Why not 为什么不

eval.call(context, 'function test(){}');

example

eval.call(window, 'function test(){}');

eval() does execute within the local scope but that isn't your problem. eval()确实在本地范围内执行,但这不是你的问题。

First, ditch eval() ! 首先,沟渠eval()

Second, think clearly on what you want without eval() . 其次,在没有eval()情况下清楚地思考你想要什么。 Even better please update the question when you have that. 更好的是,请在有问题时更新问题。

Since I can't understand the actual question here are some guesses: 由于我无法理解这里的实际问题是一些猜测:

1. Reference to a particular object 1.对特定对象的引用

If you want a reference to this of a particular object and the current context isn't sufficient Then use something like this question to "bind" (included in ecma 5) your new function to this . 如果你想参考this特定的对象和当前上下文是不够的,然后使用类似这样的问题 ,以“绑定”(包含在ECMA 5)新功能this

You'll still have reference to the local closure of course. 当然,你仍然会参考当地的关闭。

2. Function that has a specific closure 2.具有特定闭包的函数

If you want to call a function whose scope is "further out" or different than your "current scope" then define "that function" in the scope you want it to have (the closure) but then use a reference to "that function" that inner scope has 如果要调用范围“远离”或不同于“当前范围”的函数,则在您希望它具有的范围内定义“该函数” (闭包),然后使用对“该函数”的引用内在范围有

eg 例如

var test='outer';
var outer = function (){   alert(test);}

(function(){
    var test='inner';
    var inner = function(){
        alert(outer());
    }
    inner();
})()

You'll note that inner() returns "outer" in this example 您会注意到inner()在此示例中返回“outer”

在你的第二个例子中,函数test()不存在,因为它还没有定义)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM