Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #15866 > unrolled thread

Simple array reference question

Started byTuxedo <tuxedo@mailinator.com>
First post2012-09-12 12:09 +0200
Last post2012-09-12 20:59 +0200
Articles 9 — 5 participants

Back to article view | Back to comp.lang.javascript


Contents

  Simple array reference question Tuxedo <tuxedo@mailinator.com> - 2012-09-12 12:09 +0200
    Re: Simple array reference question SAM <stephanemoriaux.NoAdmin@wanadoo.fr.invalid> - 2012-09-12 12:37 +0200
      Re: Simple array reference question Tuxedo <tuxedo@mailinator.com> - 2012-09-12 13:08 +0200
        Re: Simple array reference question Stefan Weiss <krewecherl@gmail.com> - 2012-09-12 16:03 +0200
          Re: Simple array reference question Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-12 16:42 +0200
    Re: Simple array reference question marat.rheingold@gmail.com - 2012-09-12 05:34 -0700
      Re: Simple array reference question Tuxedo <tuxedo@mailinator.com> - 2012-09-12 15:10 +0200
      Re: Simple array reference question Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-12 15:36 +0200
        Re: Simple array reference question Tuxedo <tuxedo@mailinator.com> - 2012-09-12 20:59 +0200

#15866 — Simple array reference question

FromTuxedo <tuxedo@mailinator.com>
Date2012-09-12 12:09 +0200
SubjectSimple array reference question
Message-ID<k2pn0l$n7v$1@news.albasani.net>
I have two simply arrays, for example:

var birds = new Array('fowl', 'penguin', 'sparrow');
var fish = new Array('crayfish', 'jellyfish')

When accessing a page including a parameter, such as 'page.html?fish' and 
placing the string in a 'param' variable, how can the fish array be 
returned? 

var param = location.search.substring(1);

alert(param) ;

Accessed as page.html?fish the above would of course fire up the alert 
'fish' and not the actual array named fish.

Instead, how can the fish array be returned, or the birds array if that was 
used in the parameter? Would the array be copied into something new for the 
new object used in the alert? How can this best be constructed?

Thanks,
Tuxedo

[toc] | [next] | [standalone]


#15868

FromSAM <stephanemoriaux.NoAdmin@wanadoo.fr.invalid>
Date2012-09-12 12:37 +0200
Message-ID<50506600$0$21231$ba4acef3@reader.news.orange.fr>
In reply to#15866
Le 12/09/12 12:09, Tuxedo a écrit :
> I have two simply arrays, for example:
>
> var birds = new Array('fowl', 'penguin', 'sparrow');
> var fish = new Array('crayfish', 'jellyfish')
>
> When accessing a page including a parameter, such as 'page.html?fish' and
> placing the string in a 'param' variable, how can the fish array be
> returned?
>
> var param = location.search.substring(1);
>
> alert(param) ;
>
> Accessed as page.html?fish the above would of course fire up the alert
> 'fish' and not the actual array named fish.

alert(eval(param));

alert('array = ' + param +
       '\nelement 1 = ' + eval(param)[1]
      );

-- 
Stéphane Moriaux avec/with iMac-intel

[toc] | [prev] | [next] | [standalone]


#15869

FromTuxedo <tuxedo@mailinator.com>
Date2012-09-12 13:08 +0200
Message-ID<k2pqfc$uus$1@news.albasani.net>
In reply to#15868
SAM wrote:

> Le 12/09/12 12:09, Tuxedo a écrit :
> > I have two simply arrays, for example:
> >
> > var birds = new Array('fowl', 'penguin', 'sparrow');
> > var fish = new Array('crayfish', 'jellyfish')
> >
> > When accessing a page including a parameter, such as 'page.html?fish'
> > and placing the string in a 'param' variable, how can the fish array be
> > returned?
> >
> > var param = location.search.substring(1);
> >
> > alert(param) ;
> >
> > Accessed as page.html?fish the above would of course fire up the alert
> > 'fish' and not the actual array named fish.
> 
> alert(eval(param));
> 
> alert('array = ' + param +
>        '\nelement 1 = ' + eval(param)[1]
>       );
> 

Many thanks for the eval tricks!

Tuxedo

[toc] | [prev] | [next] | [standalone]


#15873

FromStefan Weiss <krewecherl@gmail.com>
Date2012-09-12 16:03 +0200
Message-ID<k2q4nb$l72$1@news.albasani.net>
In reply to#15869
On 2012-09-12 13:08, Tuxedo wrote:
> SAM wrote:
>> > When accessing a page including a parameter, such as 'page.html?fish'
>> > and placing the string in a 'param' variable, how can the fish array be
>> > returned?
...
>> alert(eval(param));
>> 
>> alert('array = ' + param +
>>        '\nelement 1 = ' + eval(param)[1]
>>       );
>> 
> 
> Many thanks for the eval tricks!

This is the very worst of all possible uses for eval. A user-supplied
parameter which is blindly eval()ed, what could possibly go wrong?

When you find that you need to access a variable based on a string
containing the variable's name, that's almost always an indication of
bad design. Yes, you could access it with this[param] in the global
scope, but it would be much cleaner to create a dedicated container for
those values:

  var data = {
      birds: ["fowl", "penguin", "sparrow"],
      fish: ["crayfish", "jellyfish"]
  };

  var param = location.search.substring(1);
  if (data.hasOwnProperty(param)) {
      // safe to use data[param]
  } else if (param) {
      // handle invalid user input
  } else {
      // no input
  }

The this[param] approach is not much better than the often abused
"register_globals" setting in PHP.

- stefan

[toc] | [prev] | [next] | [standalone]


#15874

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2012-09-12 16:42 +0200
Message-ID<3686298.zDpTOCFl0f@PointedEars.de>
In reply to#15873
Stefan Weiss wrote:

> The this[param] approach is not much better than the often abused
> "register_globals" setting in PHP.

When `this' refers to the Global Object: ACK.  Did you see that I 
recommended against that as well?


PointedEars
-- 
> If you get a bunch of authors […] that state the same "best practices"
> in any programming language, then you can bet who is wrong or right...
Not with javascript. Nonsense propagates like wildfire in this field.
  -- Richard Cornford, comp.lang.javascript, 2011-11-14

[toc] | [prev] | [next] | [standalone]


#15870

Frommarat.rheingold@gmail.com
Date2012-09-12 05:34 -0700
Message-ID<c0320514-716c-4955-8300-dae91763293f@googlegroups.com>
In reply to#15866
Use named access like 
alert(window[param]);

Details: your arrays defined in scope of window. so you can access to them not only by variable name but as window property by their names

[toc] | [prev] | [next] | [standalone]


#15871

FromTuxedo <tuxedo@mailinator.com>
Date2012-09-12 15:10 +0200
Message-ID<k2q1k3$e7e$1@news.albasani.net>
In reply to#15870
marat.rheingold@gmail.com wrote:

> Use named access like
> alert(window[param]);
> 
> Details: your arrays defined in scope of window. so you can access to them
> not only by variable name but as window property by their names

Many thanks for this detail.

Tuxedo

[toc] | [prev] | [next] | [standalone]


#15872

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2012-09-12 15:36 +0200
Message-ID<3865616.zCVvmSepPV@PointedEars.de>
In reply to#15870
marat.rheingold@gmail.com wrote:

> Use named access like
> alert(window[param]);
> 
> Details: your arrays defined in scope of window. so you can access to them
> not only by variable name but as window property by their names

Wrong.  The correct answer is that variables are properties of the *Global* 
Object.  It can be referred to reliably by `this' in the global execution 
context:

  window.alert(this[param]);

Referring to variables as properties of the host object referred to by the 
host-defined `window' property of the Global Object is implementation-
dependent and unreliable, because that object may have properties with the 
same name (it certainly *has* host-defined properties) which can shadow 
user-defined variables.

It is of course a bad idea to rely on several global variables in the first 
place, and the substring() method is insufficient here.  A viable approach 
would include code like this:

  var params = (typeof Object.create == "function")
             ? Object.create(null)
             : {};

  params.all_ = (String(window.location.search).match(/^\??(.*)/)
                  || [, ])[1];

  … params.all_ …


PointedEars
-- 
When all you know is jQuery, every problem looks $(olvable).

[toc] | [prev] | [next] | [standalone]


#15875

FromTuxedo <tuxedo@mailinator.com>
Date2012-09-12 20:59 +0200
Message-ID<k2qm3d$uqm$1@news.albasani.net>
In reply to#15872
Thomas 'PointedEars' Lahn wrote:

> marat.rheingold@gmail.com wrote:
> 
> > Use named access like
> > alert(window[param]);
> > 
> > Details: your arrays defined in scope of window. so you can access to
> > them not only by variable name but as window property by their names
> 
> Wrong.  The correct answer is that variables are properties of the
> *Global*
> Object.  It can be referred to reliably by `this' in the global execution
> context:
> 
>   window.alert(this[param]);
> 
> Referring to variables as properties of the host object referred to by the
> host-defined `window' property of the Global Object is implementation-
> dependent and unreliable, because that object may have properties with the
> same name (it certainly *has* host-defined properties) which can shadow
> user-defined variables.
> 
> It is of course a bad idea to rely on several global variables in the
> first
> place, and the substring() method is insufficient here.  A viable approach
> would include code like this:
> 
>   var params = (typeof Object.create == "function")
>              ? Object.create(null)
>              : {};
> 
>   params.all_ = (String(window.location.search).match(/^\??(.*)/)
>                   || [, ])[1];
> 
>   … params.all_ …
> 
> 
> PointedEars

Thanks for the ideas including all comments in response. I think I've got 
it done the right way now.

Tuxedo

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web