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

eval() how else

given that I have a js file included which is written programatically and I
can't change it. I would like to know how to do the following using
something other than the deprecated eval().
whats in the js file
var numArrays=something;
var data0 = new Array();
data0.name="name";
data0.data="some data";
var data1 = new Array();
data1.name="another name";
data1.data="some more data";
etc ....
function getData(arrayName) {
for ( var i=0;i<numArrays:i++) {
var el=eval('data'+i);
if (arrayName = = el.name) doSomething(el.data);
}
}

Jul 23 '05 #1
4 1904
J. J. Cale wrote on 31 aug 2004 in comp.lang.javascript:
var el=eval('data'+i);


el = window['data'+i]

[if a browser is used as javascript base]

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress,
but let us keep the discussions in the newsgroup)

Jul 23 '05 #2
J. J. Cale wrote:
given that I have a js file included which is written programatically and I
can't change it. I would like to know how to do the following using
something other than the deprecated eval().
whats in the js file
var numArrays=something;
var data0 = new Array();
data0.name="name";
data0.data="some data";
var data1 = new Array();
data1.name="another name";
data1.data="some more data";
etc ....
function getData(arrayName) {
for ( var i=0;i<numArrays:i++) {
var el=eval('data'+i); var el= window['data'+i]
// Mick
if (arrayName = = el.name) doSomething(el.data);
}
}

Jul 23 '05 #3
Lee
J. J. Cale said:

given that I have a js file included which is written programatically and I
can't change it. I would like to know how to do the following using
something other than the deprecated eval().
whats in the js file
var numArrays=something;
var data0 = new Array();
data0.name="name";
data0.data="some data";
var data1 = new Array();
data1.name="another name";
data1.data="some more data";
etc ....
function getData(arrayName) {
for ( var i=0;i<numArrays:i++) {
var el=eval('data'+i);
if (arrayName = = el.name) doSomething(el.data);
}
}


I don't know that eval() is deprecated.
It's just usually not the best way to do something.

Assuming that this is in a web page, you can replace:
eval('data'+i)
with:
window['data'+i]

The fact that the variables are defined in a .js file
isn't really significant. The contents of that file
are evaluated in the context of the current window.

Jul 23 '05 #4
J. J. Cale wrote:
given that I have a js file included which is written programatically and
I can't change it.
Why?
I would like to know how to do the following using something other than
the deprecated eval().
whats in the js file
var numArrays=something;
var data0 = new Array();
data0.name="name";
data0.data="some data";
var data1 = new Array();
data1.name="another name";
data1.data="some more data";
etc ....
function getData(arrayName) {
for ( var i=0;i<numArrays:i++) {
var el=eval('data'+i);
if (arrayName = = el.name) doSomething(el.data);
}
}


var _global = this;

function getData(arrayName)
{
for (var i = 0; i < numArrays; i++)
{
var el = _global['data' + i];
if (arrayName == el.name)
{
doSomething(el.data);
}
}
}

But:
Why do you not use an Array object in the first place?
Why do data0, data1 aso. refer to an Array object anyway?

var data = [
{name: "name",
data: "some data"},
{name: "another name",
data: "some more data"}];

Or define and use a prototype:

function Data(sName, d)
{
this.name = sName;
this.data = d;
}

var data = [
new Data("name", "some data"),
new Data("another name", "some more data")];
PointedEars

P.S.
Please use empty lines in your postings for
paragraphs to make them easier legible.
--
Our only hope lies in the mind of Kiki
Jul 23 '05 #5

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

Similar topics

9
by: HikksNotAtHome | last post by:
This is a very simplified example of an Intranet application. But, is there an easier (or more efficient) way of getting the decimal equivalent of a fraction? The actual function gets the select...
7
by: Reply Via Newsgroup | last post by:
This might sound sad... someone requesting a disertation on the 'eval' statement... but... I've been reading someone else's post - they had a huge calander like script and a handful of folk cursed...
2
by: Billy | last post by:
I want to make a calendar and add a hyperlink to the days inside the calendar. The source code is below: <body> <SCRIPT LANGUAGE="JavaScript"> var now = new Date();
12
by: knocte | last post by:
Hello. I have always thought that the eval() function was very flexible and useful. If I use it, I can define functions at runtime!! However, I have found a case where eval() does not work...
8
by: werner | last post by:
Hi! I don't want to use eval() in order to parse a user-supplied formula. What alternatives do I have? PHP has no standard functionality for tokenizing or parsing expressions in this regard. ...
3
by: idletask | last post by:
I have an SQLDataSource control, which is bound to an ASP.net 2.0 GridView control. It outputs a list of documents that a user can check in or check out of the database. In my gridview, I have...
15
by: manstey | last post by:
Hi, I have a text file called a.txt: # comments I read it using this:
5
by: Smiley | last post by:
I'm fooling around with using Eval and trying to manipulate a few things. I ran into a couple of weird results. First of all, in one place I used the following code: $filestring =...
5
by: wendallsan | last post by:
Hi all, I'm running into a situation where it seems that JS stops executing as soon as I call an eval in my script. I have an Ajax.Request call to a PHP page that builds a JS object and returns...
10
by: Gordon | last post by:
I have a script that creates new objects based on the value of a form field. Basically, the code looks like this. eval ('new ' + objType.value + '(val1, val2, val3'); objType is a select with...
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...
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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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)...
0
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...

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.