473,761 Members | 2,384 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.flatUser s = {};
events.flatUser s.Clone = "I'm the Clone property";
events.flatUser s.Edit = "I'm the Edit property";
events.flatUser s.Delete = "I'm the Delete property";

var key = "flatUsers.Clon e";

Right now, given 'events' and the key 'flatUsers.Clon e', eval is used to
retrieve events.flatUser s.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.lengt h; i++) {
context = context[arguments[i]];
}
return context;
}

window.alert(ev aluate.apply(ev ents, 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 1230
Ignacio Burgueño <ig*****@emuunl im.comwrites:
Right now, given 'events' and the key 'flatUsers.Clon e', eval is used
to retrieve events.flatUser s.Clone

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

window.alert(ev aluate.apply(ev ents, 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/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 30 '08 #2
On Jul 31, 12:02*am, Lasse Reichstein Nielsen <l...@hotpop.co mwrote:
>
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.co mwrote:
>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*****@emuunl im.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/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 31 '08 #5
Lasse Reichstein Nielsen wrote:
Ignacio Burgueño <ig*****@emuunl im.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*****@emuunl im.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.de mon.co.uk>
Aug 1 '08 #7

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

Similar topics

2
2245
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
3570
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
6044
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 Class A {
4
2008
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 have solutions, none of them seems really "clean" to me and I was wondering if someone here maybe had a better idea: class geometry { ... }
40
3154
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 fcn is a boolean and there are certain places within the inner functions where it may become apparent that the return value is false. In this case I'd like to halt the computation immediately and return false. My current approach is to have...
15
1711
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
9522
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
10111
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9948
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
9902
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,...
0
8770
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5215
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...
0
5364
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3866
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
3
2738
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.