473,385 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Controlling the bindings within an eval

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
4 1960
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Doug | last post by:
The html below shows DataList "DiscountList" nested within DataList "EventItemList". DiscountList contains a Label control. I'm trying to find the label, using FindControl, during...
3
by: CVerma | last post by:
Hi, I have an embedded datagrid within a datalist. I am not able to perfrom paging in the datagrid. Any ideas? Here is my code: Here is my Simplegrid.cs file: using System; using...
0
by: Luis Esteban Valencia | last post by:
am trying to databind within an asp:textbox control. I have tried many variations found here but nothing seems to work. It works fine if I use a regular HTML textbox input though. Here is what I...
4
by: Girish | last post by:
Im trying to create a grid within a grid programmatically. Ive been successful in doing this but I need the embedded grid to fire its ItemDataBound event so I can handle it. The event does not seem...
2
by: mikepolitowski | last post by:
Hi folks, I am have been trying to solve this problem for quite some time now and would appreciate any advice. I have been trying to call a code-behind function that is defined in my aspx.cs...
2
by: mark4asp | last post by:
How do I include a server tag within a javascript parameter which is itself within a HTML element event. For instance no matter what permutation of quotes I try here I get errors on this line: ...
0
by: Tor Inge Rislaa | last post by:
Controlling TreeView Expand/Collapse With a TreeView bound to an XML file as below I want to obtain the functionality where Chapter 2 and 3 are forced to collapse when Chapter 1 is...
6
by: =?Utf-8?B?QmVuLg==?= | last post by:
Hi, I'm more of a windows programmer than ASP and I've having a little difficulty in ASP.Net (framework 2.0) - VB.net I have a datagrid on an aspx page and the html is building the display in...
2
by: Bruce | last post by:
From within a class instantiation, I'm trying to set the value of a global variable. Let's call that variable "x". I know I can do this window.x = 5; and it will work but I really need to do...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.