473,799 Members | 3,146 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(someO bj, "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 1988
"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(someO bj, "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(thisObje ct, code) {
return (function(){ret urn eval(code);}).c all(thisObject) ;
}

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

/L
--
Lasse Reichstein Nielsen
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 10 '08 #2
On Jul 10, 12:11*am, Lasse Reichstein Nielsen <l...@hotpop.co mwrote:
"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(someO bj, "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(thisObje ct, code) {
* *return (function(){ret urn eval(code);}).c all(thisObject) ;
*}

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

/L
--
Lasse Reichstein Nielsen
*DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.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.co mwrote:
"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(someO bj, "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(thisObje ct, code) {
* *return (function(){ret urn eval(code);}).c all(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.prototyp e and using a
catch clause:-

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

function removeFromOp(o) {
var op = Object.prototyp e, p;
for(p in o) {
if(preexist.has OwnProperty(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**********@g mail.comwrites:
On Jul 9, 10:11*pm, Lasse Reichstein Nielsen <l...@hotpop.co mwrote:
>*function myEval(thisObje ct, code) {
* *return (function(){ret urn eval(code);}).c all(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){ret urn 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/rasterTriangleD OM.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
3185
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 EventList_ItemCreated (below the html), but it's always <undefined value> (null). Everything else works fine. Eventually I need to set the value of the label depending up the Count of the DataView "dvDiscount". For now I'll settle for just finding the damn...
3
2788
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 System.Collections; using System.ComponentModel; using System.Data;
0
1441
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 have tried: <asp:textbox id='<%#DataBinder.Eval(Container.DataItem, "EmailID")%>' cssclass="textbox" runat="server"><%#DataBinder.Eval(Container.DataItem, "Email")%></asp:textbox> I get '<%# DataBinder.Eval(Container.DataItem,...
4
4485
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 to fire for some reason. The code is below. Look towards the end of the CustomerDataGrid_OnItemDataBound(object sender, DataGridItemEventArgs e) method. This is where I register the event and place the programmatically created grid into the...
2
3552
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 from within a DataList <ItemTemplate> block using the <%# %> syntax. I would not have written here if I had not spent over 6 hours trying to find a solution to this problem again any advice is greatly appreciated. I have included a code snippet...
2
3396
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: <asp:ImageButton ID="cmdDeleteLogin" runat="server" SkinID="DeleteButton" CommandName="DeleteLogin" CommandArgument='<%# Eval("LoginID") %>' OnClientClick =
0
2349
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 selected and Chapter 1 and 3 collapses when Chapter 2 is selected and so on.
6
1415
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 a table row by row. A sample row being <tr> <td>Comment:</td>
2
3992
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 this: x = 5; The above works in Firefox 2 but not in IE6. Actually, I'm trying to
0
9688
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10260
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10243
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7570
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6809
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4146
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.