By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,598 Members | 1,894 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,598 IT Pros & Developers. It's quick & easy.

Controlling the bindings within an eval

P: n/a
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?
Jul 10 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Adam C." <ad*****@gmail.comwrites:
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
--
Lasse Reichstein Nielsen
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 10 '08 #2

P: n/a
On Jul 10, 12:11*am, Lasse Reichstein Nielsen <l...@hotpop.comwrote:
"Adam C." <adam...@gmail.comwrites:
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
--
Lasse Reichstein Nielsen
*DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
* 'Faith without judgement merely degrades the spirit divine.'
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?
Jul 10 '08 #3

P: n/a
On Jul 9, 10:11*pm, Lasse Reichstein Nielsen <l...@hotpop.comwrote:
"Adam C." <adam...@gmail.comwrites:
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
Jul 10 '08 #4

P: n/a
dhtml <dh**********@gmail.comwrites:
On Jul 9, 10:11*pm, Lasse Reichstein Nielsen <l...@hotpop.comwrote:
>*function myEval(thisObject, code) {
* *return (function(){return eval(code);}).call(thisObject);
*}
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
--
Lasse Reichstein Nielsen
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 10 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.