473,322 Members | 1,188 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,322 software developers and data experts.

Getting rid of eval for accesing "deep" properties

Hi everyone.

I'm dealing with some javascript code which uses eval to access
properties of an object.
For instance, I have the following:

var events = {};
events.flatUsers = {};
events.flatUsers.Clone = "I'm the Clone property";
events.flatUsers.Edit = "I'm the Edit property";
events.flatUsers.Delete = "I'm the Delete property";

var key = "flatUsers.Clone";

Right now, given 'events' and the key 'flatUsers.Clone', eval is used to
retrieve events.flatUsers.Clone

window.alert( eval("events." + key) );

I'd like to get rid of eval, and since I cannot do just:
events[key]

I wrote the following:
function evaluate() {
var context = this;
for(var i = 0; i < arguments.length; i++) {
context = context[arguments[i]];
}
return context;
}

window.alert(evaluate.apply(events, key.split('.')));

Surely this can be improved, since I'm a newbie in Javascript. Any
suggestions?

Regards,
Ignacio Burgueño
Jul 30 '08 #1
6 1203
Ignacio Burgueño <ig*****@emuunlim.comwrites:
Right now, given 'events' and the key 'flatUsers.Clone', eval is used
to retrieve events.flatUsers.Clone

window.alert( eval("events." + key) );
....
I wrote the following:
function evaluate() {
var context = this;
for(var i = 0; i < arguments.length; i++) {
context = context[arguments[i]];
}
return context;
}

window.alert(evaluate.apply(events, key.split('.')));

Surely this can be improved, since I'm a newbie in Javascript. Any
suggestions?
Looks a little on the overkill side, but not far from what I would do:

function getProperty(obj, propPath) {
var parts = propPath.split(/\./g);
for(var i = 0; i < parts.length; i++) {
obj = obj[parts[i]];
}
return obj;
}

/L
--
Lasse Reichstein Nielsen
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 30 '08 #2
On Jul 31, 12:02*am, Lasse Reichstein Nielsen <l...@hotpop.comwrote:
>
function getProperty(obj, propPath) {
*var parts = propPath.split(/\./g);
*for(var i = 0; i < parts.length; i++) {
* *obj = obj[parts[i]];
*}
*return obj;

}
Or

function getProperty (obj, parts) {
parts= parts.split(/\./g);
while (parts.length) { obj= obj[parts.shift()] }
return obj;
}

--Jorge.
Jul 30 '08 #3
Jorge wrote:
On Jul 31, 12:02 am, Lasse Reichstein Nielsen <l...@hotpop.comwrote:
>function getProperty(obj, propPath) {
var parts = propPath.split(/\./g);
for(var i = 0; i < parts.length; i++) {
obj = obj[parts[i]];
}
return obj;

}

Or

function getProperty (obj, parts) {
parts= parts.split(/\./g);
while (parts.length) { obj= obj[parts.shift()] }
return obj;
}

--Jorge.

Thanks both!
I was curious about the performance penalty. If case there's any
interest, here are the times it took to run 100.000 times each method
(eval, my first attempt (I called it 'evaluate') and Jorge's getProperty
variation)
It seems that in this particular (and simple) case, there's not a huge
performance penalty by using eval.

| IE7 | IE6 |Opera |Safari| FF2 | FF3 |IE7(2)|
------------+------+------+------+------+------+------+------|
eval | 1468 | 2953 | 1547 | 812 | 4625 | 1151 | 8188 |
------------+------+------+------+------+------+------+------|
evaluate | 2110 | 4469 | 609 | 704 | 3828 | 676 | 2109 |
------------+------+------+------+------+------+------+------|
getProperty | 2234 | 5187 | 1047 | 343 | 3563 | 709 | 2250 |
-------------------------------------------------------------/
IE7 = 7.0.5730.11
IE7(2) = The same, but with script debugging enabled
IE6 = 6.0.2900.2180
Opera = 9.51.10081
Safari = 3.1.2 (525.21)
FF3 = 3.0.1
FF2 = 2.0.0.16

Tests in Firefox were run with all extensions disabled.

Regards,
Ignacio Burgueño
Jul 31 '08 #4
Ignacio Burgueño <ig*****@emuunlim.comwrites:
It seems that in this particular (and simple) case, there's not a huge
performance penalty by using eval.
Performance is not the (primary) reason to avoid "eval". A much
bigger problem is that code crated by putting strings together at
runtime is often fragile, and when it fails, it's hard to debug.

/L
--
Lasse Reichstein Nielsen
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 31 '08 #5
Lasse Reichstein Nielsen wrote:
Ignacio Burgueño <ig*****@emuunlim.comwrites:
>It seems that in this particular (and simple) case, there's not a huge
performance penalty by using eval.

Performance is not the (primary) reason to avoid "eval". A much
bigger problem is that code crated by putting strings together at
runtime is often fragile, and when it fails, it's hard to debug.

/L
Indeed, you're right. I'd never use eval if I weren't sure where the
code came from. I don't like the way it's being used in my code, but,
well, I can't change that at the moment.

Thanks for your help, Lasse.

Regards,
Ignacio Burgueño
Aug 1 '08 #6
Ignacio Burgueño wrote:
Lasse Reichstein Nielsen wrote:
>Ignacio Burgueño <ig*****@emuunlim.comwrites:
>>It seems that in this particular (and simple) case, there's not a huge
performance penalty by using eval.
Performance is not the (primary) reason to avoid "eval". A much
bigger problem is that code crated by putting strings together at
runtime is often fragile, and when it fails, it's hard to debug.

Indeed, you're right. I'd never use eval if I weren't sure where the
code came from. I don't like the way it's being used in my code, but,
well, I can't change that at the moment.
You have been shown how to change it right now.
PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>
Aug 1 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Nick Jacobson | last post by:
This question is with regard to the * operator as used for sequence concatenation. There's the well-known gotcha: a = ] b = a*3 b = 4 print b
1
by: Alfonso Morra | last post by:
Hi, I have the ff data types : typedef enum { VAL_LONG , VAL_DOUBLE , VAL_STRING , VAL_DATASET }ValueTypeEnum ;
1
by: Jesper Denmark | last post by:
Hi, Is deep serialization possible using XML. Know any good tutorials?. By deep serialization I mean the ability that an object being serialized automatically serialize its members. E.g ...
4
by: lars.uffmann | last post by:
Hey everyone! I am (still) working on a project that I took over from former students, so don't blame me for the criminal approach on coding *g* The problem I have is fairly easy and while I...
40
by: Mark P | last post by:
I'm implementing an algorithm and the computational flow is a somewhat deep. That is, fcn A makes many calls to fcn B which makes many calls to fcn C, and so on. The return value of the outermost...
15
by: Matt Kruse | last post by:
Consider the following two functions: function func1() { var o ; if ( (o=document.forms) && (o=o) && (o=o.elements) && (o=o.sel) && (o=o.options) && (o=o)
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.