简体   繁体   中英

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)

//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

If I defined myfunction within the outer eval I get this error: object is not a function

//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. 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. 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.

//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. 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...

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.

First, ditch eval() !

Second, think clearly on what you want without 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

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 .

You'll still have reference to the local closure of course.

2. Function that has a specific closure

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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