Controlling the bindings within an eval

A

Adam C.

Mozilla.org suggests using the with statement to control bindings:

var f = 2;

with({f: 3}){
eval("f"); // evaluates to 3
}

But that doesn't work for binding "this".

setTimeout (with a string to be evaluated) will evaluate "this" as the
window.

eval.call(someObj, "this")
returns an error in Firefox; in IE is seems to just bind "this" to the
window.

Is there a way to completely control the bindings, including this, of
the code to be evaluated?
 
L

Lasse Reichstein Nielsen

Adam C. said:
Mozilla.org suggests using the with statement to control bindings:

var f = 2;

with({f: 3}){
eval("f"); // evaluates to 3
}

Ick. Don't use eval! Don't use with!
The above is the same as {f:3}["f"]
But that doesn't work for binding "this".

Indeed, "this" is not a variable, it's an operator.
setTimeout (with a string to be evaluated) will evaluate "this" as the
window.

eval.call(someObj, "this")
returns an error in Firefox; in IE is seems to just bind "this" to the
window.

Eval called as anything but a method on the global object is not
guaranteed to work.

The above is equal to just: someObj.
Is there a way to completely control the bindings, including this, of
the code to be evaluated?

Try:

function myEval(thisObject, code) {
return (function(){return eval(code);}).call(thisObject);
}

But really ... try to avoid needing it instead!

/L
 
A

Adam C.

Adam C. said:
Mozilla.org suggests using the with statement to control bindings:
var f = 2;
with({f: 3}){
  eval("f"); // evaluates to 3
}

Ick. Don't use eval! Don't use with!
The above is the same as {f:3}["f"]
But that doesn't work for binding "this".

Indeed, "this" is not a variable, it's an operator.
setTimeout (with a string to be evaluated) will evaluate "this" as the
window.
eval.call(someObj, "this")
returns an error in Firefox; in IE is seems to just bind "this" to the
window.

Eval called as anything but a method on the global object is not
guaranteed to work.

The above is equal to just: someObj.
Is there a way to completely control the bindings, including this, of
the code to be evaluated?

Try:

 function myEval(thisObject, code) {
   return (function(){return eval(code);}).call(thisObject);
 }

But really ... try to avoid needing it instead!

/L

Nice solution; thanks!. Try not to assume the worst on so little
evidence, though; eval("f") was just a simple example to make the
issue clear, not an example of the intended usage. A colleague is
working on an in-house JavaScript loading framework, and evaluating
JavaScript brought back by Ajax is one of the alternatives being
considered. Although we have no connection to the author,
http://www.west-wind.com/WebLog/posts/413878.aspx gives a good
overview of the problem space.

Just for my further education: Obviously eval is not to be preferred
when there are more efficient alternatives. I can also see big
security issues -- you need to be sure that you are evaluating safe
code. Is there any other reason to avoid eval?
 
D

dhtml

Adam C. said:
Mozilla.org suggests using the with statement to control bindings:
var f = 2;
with({f: 3}){
  eval("f"); // evaluates to 3
}

Ick. Don't use eval! Don't use with!
The above is the same as {f:3}["f"]
But that doesn't work for binding "this".

Indeed, "this" is not a variable, it's an operator.
setTimeout (with a string to be evaluated) will evaluate "this" as the
window.
eval.call(someObj, "this")
returns an error in Firefox; in IE is seems to just bind "this" to the
window.

Eval called as anything but a method on the global object is not
guaranteed to work.

The above is equal to just: someObj.
Is there a way to completely control the bindings, including this, of
the code to be evaluated?

Try:

 function myEval(thisObject, code) {
   return (function(){return eval(code);}).call(thisObject);
 }


How would this work?

alert(myEval( {x:1}, "x"));

Would be a ReferenceError.

This isn't what you intended, is it? The - thisObject would be the
context, and would not be added to the [[Scope]] and certainly would
not augment the scope (as the OP's example using with does).

There might be a way of messing with Object.prototype and using a
catch clause:-

(function(){
var preexist = {};
function addToOp(o) {
var op = Object.prototype, p;
for(p in o) {
if(p in op)
preexist[p] = op[p];
op[p] = o[p];
}
}

function removeFromOp(o) {
var op = Object.prototype, p;
for(p in o) {
if(preexist.hasOwnProperty(p))
op[p] = preexist[p];
else
delete op[p];
}
}

this.ScopedEval = function(o, p) {
try { addToOp(o); throw 0; }
catch(f) {
ret = eval(p);
removeFromOp(o);
return ret;
}
}
})();


- but with would be much cleaner.

Garrett
But really ... try to avoid needing it instead!

Yes,

obj[prop] is the way to go!
 
L

Lasse Reichstein Nielsen

How would this work?

alert(myEval( {x:1}, "x"));

Would be a ReferenceError.

Yes, but
alert(myEval({x:1},"this.x"));
would alert "1". The exercise was to bind the "this" operator in
eval-code.

It was not an attempt to emulate "with" (the "with" construction
already does that badly enough).


However, it does have the side-effect of removing the current scope.
If you need both the current scope and the current "this" object,
then you'll need to inline the construction, so the function around
the "eval" retains the scope chaing:

function f(foo) {
this.bar = 42;

var res = function(c){return eval(c);}.call(this,"foo + this.bar");
alert(res);
}
f(37); // alerts 79


/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top