3

On dynamic property of JavaScript is that "eval" changes the calling context. What does it mean? Some examples would be better.

3
  • 5
    Please add the source from where you read the above mentioned statement. Commented Jan 1, 2012 at 17:07
  • eval scopes to the calling context. See this for some code. It means the context in which eval is called is the context that the code being evaluated will execute in. Commented Jan 1, 2012 at 17:08
  • langnetsymposium.com/2009/talks/18-LarsBak-JavaScript.html Commented Jan 1, 2012 at 18:06

3 Answers 3

4

eval does change the context when called indirectly. And it changes it to the global context (the default context for all functions).

var myObj = { a: 1 }

function someFunc() {
    console.log(eval('this.a')) // 1
    console.log(eval('this === myObj')) // true

    var indirectEval = eval

    console.log(indirectEval('this.a')) // undefined
    console.log(indirectEval('this === window')) // true
}

void someFunc.call(myObj)

Direct eval calls don't change the context (nor do they change the scope).

See “Global eval. What are the options?” for details.

Sign up to request clarification or add additional context in comments.

Comments

3

http://jsfiddle.net/9h6n7/1/

The "a" variable is part of the context, and the eval changes it.
The calling context is the variables that surround the eval call, in our case just the "a" variable and "this" that in the browser equals the window.

var a = 1;
eval("a = 2;");
alert(a); // 2

An example that is more clear http://jsfiddle.net/9h6n7/2/ :

function executeEval(){
  eval("a = 2;");
}

var a = 1;
executeEval();
alert(a); // 2

2 Comments

Your definition of calling context is rather different from my understanding, which is a sequence of function calls. I'd wait a bit for clearer answers.
calling context means all the variable accessible from the scope where the call is made: "this", local variables, closure captured variables, global variables.
0

I assume you mean the execution context, not the calling context.

Explaining it here doesn't seem to make much sense, since there are many online articles dealing with this and good JS books such as Javascript, The Good Parts also deal with it.

Here's a link which came top in Google: http://ajax.sys-con.com/node/676031

4 Comments

The linked text can't be read, a huge ad covers all the page.
@katspaugh, not on my browser, but in any case, maybe the print version works better: ajax.sys-con.com/node/676031/print
Gosh, they have a smaller ad even in the print version. It's much better, though. Thanks!
I surely agree with you that I can google it to get a lot of information. But the problem is that I need to "mine" it to find the piece that is really valuable to me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.