eval() how else

L

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.
 
M

Mick White

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);
var el= window['data'+i]
// Mick
 
J

J. J. Cale

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);
}
}
 
T

Thomas 'PointedEars' Lahn

J. J. Cale said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top