From: williamc on

[snipped]

My reply...
>> Ok. So if I'm understanding correctly, running the eval as above is very
>> similar to using a function expression like...

Garrett's reply to that...
> No. When eval is called with a string argument, an execution context is
> entered using the what is known as the "calling context's" scope and
> `this` value. If eval was called in global context, then the `this`
> value and variable object is the global object.

Thanks, I'll read the reference material you mentioned.

What if I had said 'running the eval as above is very similar to using
the the function constructor at that point in the code'?

I've read in various sources that the function constructor is in effect
like a call to Eval, but have never really thought about it before,
since I've literally never used eval.

So, instead of...

eval("function x() { alert('x is available!'); }");
x();

you have...

var x = new Function("alert('x is available!');");
x();

Are those near equivalents?

--williamc
From: VK on
On May 9, 2:31 am, Dr J R Stockton <reply1...(a)merlyn.demon.co.uk>
wrote:

> typeof eval("T=function F(){return 3}")

var T = new Function("return 3;");

or say with named arguments:

var T = new Function("arg1", "arg2", "return arg1 + arg2;");

Universally uniformly supported since Netscape 2.x

From: VK on
On May 9, 3:42 pm, williamc <n...(a)nowhere.net> wrote:
> So, instead of...
>
> eval("function x() { alert('x is available!'); }");
> x();
>
> you have...
>
> var x = new Function("alert('x is available!');");
> x();
>
> Are those near equivalents?

Pretty much, though the 2nd is definitely preferred over the 1st one:
the rule is to use for a particular task the language tool provided
for that exact task, use custom tools only if that tool implementation
is broken - which is not the case of any kind for Function
constructor.

As a small yet important difference to remember is that Function
constructor implements deferred parsing: the code is actually parsed
on the first function call, not on function instantiation. This way:
eval("function x() { !@#$%^& }");
will throw syntax error right away, while:
x = new Function("!@#$%^&");
will be fine until you try for the first time:
x();


From: Ry Nohryb on
On May 9, 1:42 pm, williamc <n...(a)nowhere.net> wrote:
> [snipped]
>
> My reply...
>
> >> Ok. So if I'm understanding correctly, running the eval as above is very
> >> similar to using a function expression like...
>
> Garrett's reply to that...
>
> > No. When eval is called with a string argument, an execution context is
> > entered using the what is known as the "calling context's" scope and
> > `this` value. If eval was called in global context, then the `this`
> > value and variable object is the global object.
>
> Thanks, I'll read the reference material you mentioned.
>
> What if I had said 'running the eval as above is very similar to using
> the the function constructor at that point in the code'?
>
> I've read in various sources that the function constructor is in effect
> like a call to Eval, but have never really thought about it before,
> since I've literally never used eval.
>
> So, instead of...
>
> eval("function x() { alert('x is available!'); }");
> x();
>
> you have...
>
> var x = new Function("alert('x is available!');");
> x();
>
> Are those near equivalents?

Yes and no. Yes because both return a function. No because the
function that eval returns has the current scope in scope, unlike the
one that Function() returns:

var a= 27;
(function () {
var a= 10;
var f1= Function("return a;");
var f2= eval("(function () { return a })");
alert ([f1(), f2()]);
})()

--> [27, 10]
--
Jorge.
From: Garrett Smith on
williamc wrote:
> [snipped]
>
> My reply...
>>> Ok. So if I'm understanding correctly, running the eval as above is very
>>> similar to using a function expression like...
>
> Garrett's reply to that...
>> No. When eval is called with a string argument, an execution context is
>> entered using the what is known as the "calling context's" scope and
>> `this` value. If eval was called in global context, then the `this`
>> value and variable object is the global object.
>
> Thanks, I'll read the reference material you mentioned.
>
> What if I had said 'running the eval as above is very similar to using
> the the function constructor at that point in the code'?
>

It sounds like you're guessing.

To understand the language on how it is interpreted, put yourself in
first person to think as the interpreter. Can you explain, for example:

function x(){
var a;
eval("a = 19");
}

x();

When x is called, what happens? Review the section "entering an
execution context".

function y(){
var a;
var f = new Function("a = 19");
return f;
}

var bb = y();

What happens here? What happens when bb is called?

> I've read in various sources that the function constructor is in effect
> like a call to Eval, but have never really thought about it before,
> since I've literally never used eval.
>

Function constructor is like eval in that evaluates a string of code as
SourceElements.

> So, instead of...
>
> eval("function x() { alert('x is available!'); }");
> x();
>
> you have...
>
> var x = new Function("alert('x is available!');");
> x();
>
> Are those near equivalents?
>
No.

The first has a call to eval. The evaluated string has the expected
outcome as explained in detail to you previously, adding an `x` property
to the VO.

The second is a call to the Function constructor which creates and new
function whose scope is global.
--
Garrett
comp.lang.javascript FAQ: http://jibbering.com/faq/