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


Groups > comp.lang.javascript > #36659

Re: Create array using name from string?

Path csiph.com!3.eu.feeder.erje.net!feeder.erje.net!news.albasani.net!.POSTED!not-for-mail
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: Create array using name from string?
Date Wed, 29 May 2019 00:22:27 +0200
Organization PointedEars Software (PES)
Lines 52
Message-ID <5255243.DvuYhMxLoT@PointedEars.de> (permalink)
References <38042cf3-73ca-403a-b13b-7bcfbec21ffb@googlegroups.com> <qcivl4$119l$1@gioia.aioe.org> <qcj10g$17lk$1@gioia.aioe.org> <84fe9111-5bab-4537-8d08-424ec56763d7@googlegroups.com> <87h89elg1u.fsf@bsb.me.uk>
Reply-To Thomas 'PointedEars' Lahn <cljs@PointedEars.de>
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
Content-Transfer-Encoding 8Bit
X-Trace news.albasani.net RVQuxh5FTtNOTRzQyblOCr9zZt5l4lXaimkekS+1CDqjBG6is72KzU/EaLCGNLipTypSLaY7468Ioj89OL2mnBMoHqcGpXA/G2uWpkwgUth6wStLVxRO9AzSoPeDTKHN
NNTP-Posting-Date Tue, 28 May 2019 22:22:28 +0000 (UTC)
Injection-Info news.albasani.net; logging-data="mG0pPzHmu4oevqJ7YOlW68+9UE2g+RBWwk9nOwAqbkW0Zn3wXDEcxAAaJdXwkRvsNORFI8R8DiENQhbW0WJ12rXAPuwj/bKhQ5tuaiyFBIaiIE6EZuHUkyMr0qKDP7O5"; mail-complaints-to="abuse@albasani.net"
User-Agent KNode/4.14.10
Face iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg==
X-Face %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v
Cancel-Lock sha1:W4JevXJUJTVh53OvNNQcCyOmV14=
Xref csiph.com comp.lang.javascript:36659

Show key headers only | View raw


Ben Bacarisse wrote:

> dr.j.r.stockton@gmail.com writes:
>> On Tuesday, 28 May 2019 11:03:03 UTC+1, R.Wieser  wrote:
>>> Forget what I said.   I just noticed that this is the javascript
>>> newsgroup, and not the VBScript one.  My bad.
>>
>> No matter - your reply only needed translation :-
>>
>>   S = "Banana"
>>   eval("var " + S + " = [2, 9, +new Date()]")
>>   Banana // is now a three-element array
> 
> Mind you, it's worth pointing out, I think, that this sort of code is
> widely regarded as a Very Bad Thing.  The OP is immune to such
> suggestions but there may be innocent learners reading this!

To elaborate, the posted code will *not work* in strict mode:

  1. “S” as posted is an undeclared identifier; this will throw a
     ReferenceError exception.

  2. If the first problem is fixed, the code will still not work as
     described because in strict mode declarations in eval execution context
     do not propagate to the calling context: a ReferenceError exception
     will be thrown on resolving “Banana” even though it would be declared
     in the eval context.

In non-strict mode, the code will work to some extent but exhibit side 
effects such as overwriting properties of objects in the scope chain and may 
be prone to code injection because of eval().

In addition, using eval() here is not only less efficient, but completely 
pointless (but I guess that was the point):

  var banana = [2, 9, +new Date()];

“(new Date()).getTime()” is arguably less efficiently and less elegantly 
written, but more explicit and more efficiently processed, than “+new 
Date()”; the latter would be because evaluating it takes fewer detours 
through the ECMAScript algorithm jungle.

Finally, I have replaced “Banana” with “banana” because it does not refer to 
a constructor here.  (Unfortunately, regarding his code style Dr. Stockton 
appears to be stuck the world of Borland Pascal where such distinctions
need not be made because it is not an OOPL in the first place.)

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Create array using name from string? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2019-05-28 02:06 -0700
  Re: Create array using name from string? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2019-05-28 02:11 -0700
  Re: Create array using name from string? "R.Wieser" <address@not.available> - 2019-05-28 11:39 +0200
    Re: Create array using name from string? "R.Wieser" <address@not.available> - 2019-05-28 12:02 +0200
      Re: Create array using name from string? dr.j.r.stockton@gmail.com - 2019-05-28 05:38 -0700
        Re: Create array using name from string? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-05-28 17:06 +0100
          Re: Create array using name from string? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2019-05-29 00:22 +0200
            Re: Create array using name from string? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2019-05-29 00:07 -0700
              Re: Create array using name from string? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2019-05-29 09:47 +0200
              Re: Create array using name from string? Julio Di Egidio <julio@diegidio.name> - 2019-05-29 10:10 -0700
                Re: Create array using name from string? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2019-05-30 11:10 -0700
                Re: Create array using name from string? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2019-05-30 11:12 -0700
                Re: Create array using name from string? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2019-05-30 11:22 -0700
                Re: Create array using name from string? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2019-05-30 11:28 -0700
            Re: Create array using name from string? Julio Di Egidio <julio@diegidio.name> - 2019-05-29 10:24 -0700
              Re: Create array using name from string? Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2019-05-30 11:37 +0200
                Re: Create array using name from string? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2019-05-30 12:35 +0200
                Re: Create array using name from string? Julio Di Egidio <julio@diegidio.name> - 2019-05-30 03:43 -0700
                Re: Create array using name from string? Julio Di Egidio <julio@diegidio.name> - 2019-05-30 03:48 -0700
                Re: Create array using name from string? Gene Wirchenko <genew@telus.net> - 2019-05-30 12:13 -0700
                Re: Create array using name from string? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2019-05-30 10:34 -0700
              Re: Create array using name from string? John G Harris <niam@jghnorth.org.uk.invalid> - 2019-05-31 19:01 +0100
                Re: Create array using name from string? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2019-05-31 11:21 -0700
                Re: Create array using name from string? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2019-06-01 00:31 +0200
                Re: Create array using name from string? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2019-06-01 00:40 -0700
                Re: Create array using name from string? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2019-06-01 01:10 -0700
                Re: Create array using name from string? John G Harris <niam@jghnorth.org.uk.invalid> - 2019-06-01 10:14 +0100
        Re: Create array using name from string? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2019-05-29 00:15 -0700
          Re: Create array using name from string? "R.Wieser" <address@not.available> - 2019-05-29 09:35 +0200
            Re: Create array using name from string? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2019-05-29 01:16 -0700

csiph-web