Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #22374
| From | Joao Rodrigues <groups_jr-1@yahoo.com.br> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: Date Conversion ? |
| Date | 2013-12-23 10:00 -0200 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <l998k2$rhr$1@speranza.aioe.org> (permalink) |
| References | <bh9en6Foh76U1@mid.individual.net> <5f4va916l0r2qoto1r4iq5dh0i0dnqthnv@hive.bjoern.hoehrmann.de> <3082498.rgpu3afg7L@PointedEars.de> <23946178.EFlsh5Ejh5@PointedEars.de> |
On 20/12/2013 19:29, Thomas 'PointedEars' Lahn wrote:
> Thomas 'PointedEars' Lahn wrote:
>
>> Bjoern Hoehrmann wrote:
<snip>
>> you can do much the same as in Perl:
>>
>> […]
>> /*
>> * or
>>
>> var m = ({
>> "Jan": 1,
>> "Feb": 2,
>> …
>> })[matches.groups["m"]];
>>
>> * with JSX:regexp.js [2]
>> */
>> var m = ({
>> "Jan": 1,
>> "Feb": 2,
>> …
>> })[matches[0]];
>
> var m = {
> "Jan": 1,
> "Feb": 2,
> …
> }[matches[0]];
>
> (and similar; without parentheses) suffices because the right-hand side of
> the assignment is evaluated in Expression context, not Statement context.
>
> This is different with
>
> var s = '{"foo": "bar"}';
>
> /* Syntax error in the /Program/ parsed by eval() */
> var o = eval(s):
>
> /* Correct syntax in the /Program/ parsed by eval() */
> var o = eval("(" + s + ")"):
>
> – as an insecure but backwards-compatible stand-in for JSON.parse() – and
> when for some reason you want to access a specific object referred by
> another object's property, based on a name:
>
> var s = "foo";
>
> {
> "foo": ["baz", "bar"]
> }[s].sort();
>
> is a Syntax error;
>
> var s = "foo";
>
> ({
> "foo": ["baz", "bar"]
> })[s].sort();
>
> is not.
It is so because an Expression cannot start with "function " or "{"
<http://ecma-international.org/ecma-262/5.1/#sec-12.4>
Besides the surrounding parentheses, we can use the comma operator:
<http://ecma-international.org/ecma-262/5.1/#sec-11.14>
var s = '{"foo": "bar"}';
var o = eval("1," + s);
Or:
var s = "foo";
1, {
"foo": ["baz", "bar"]
}[s].sort();
--
Joao Rodrigues (J.R.)
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Date Conversion ? "Mel Smith" <med_cutout_syntel@aol.com> - 2013-12-16 15:55 -0700
Re: Date Conversion ? Bjoern Hoehrmann <bjoern@hoehrmann.de> - 2013-12-17 01:06 +0100
Re: Date Conversion ? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-12-19 12:34 +0100
Re: Date Conversion ? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-12-20 22:29 +0100
Re: Date Conversion ? Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2013-12-23 10:00 -0200
Re: Date Conversion ? JJ <duh@nah.meh> - 2013-12-17 07:06 +0700
Re: Date Conversion ? Denis McMahon <denismfmcmahon@gmail.com> - 2013-12-17 00:35 +0000
Re: Date Conversion ? "Mel Smith" <med_cutout_syntel@aol.com> - 2013-12-16 18:16 -0700
Re: Date Conversion ? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-12-17 10:09 +0100
Re: Date Conversion ? Denis McMahon <denismfmcmahon@gmail.com> - 2013-12-17 17:02 +0000
Re: Date Conversion ? Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-12-18 19:14 +0000
Re: Date Conversion ? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-12-19 15:42 +0100
Re: Date Conversion ? "Michael F. Stemper" <michael.stemper@gmail.com> - 2013-12-19 09:20 -0600
Re: Date Conversion ? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-12-20 10:04 +0100
Re: Date Conversion ? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-12-17 15:12 +0100
Re: Date Conversion ? Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2013-12-17 18:10 -0200
Re: Date Conversion ? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-12-17 21:42 +0100
Re: Date Conversion ? Bjoern Hoehrmann <bjoern@hoehrmann.de> - 2013-12-18 13:36 +0100
Re: Date Conversion ? Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-12-17 19:02 +0000
Re: Date Conversion ? "Mel Smith" <med_cutout_syntel@aol.com> - 2013-12-17 20:51 -0700
Re: Date Conversion ? Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-12-19 20:21 +0000
Re: Date Conversion ? "Mel Smith" <med_cutout_syntel@aol.com> - 2013-12-20 09:52 -0700
Re: Date Conversion ? Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-12-21 20:41 +0000
Re: Date Conversion ? "Mel Smith" <med_cutout_syntel@aol.com> - 2013-12-22 09:52 -0700
csiph-web