|
| Variable names in JSON syntax (Javascript) |
 |
Mon, 16 Jul 2007 15:54:08 +010 |
Why does the following Javascript display "defined/undefined" rather
than
"undefined/defined"?
prop='myString'
var obj={prop:'defined'}
alert(obj.prop+'/'+obj.myString)
According to any specs/examples I can find, JSON property names should be
in quotes so wouldn't this mean the unquoted string 'prop' should be
expanded to its value ('myString'), just like it would using array syntax:
obj[prop]='value'
|
| Post Reply
|
| Re: Variable names in JSON syntax (Javascript) |
 |
Fri, 20 Jul 2007 11:30:52 +020 |
On Mon, 16 Jul 2007 15:54:08 +0100, Eik wrote:
> Why does the following Javascript display "defined/undefined"
rather than
> "undefined/defined"?
>
> prop='myString'
> var obj={prop:'defined'}
> alert(obj.prop+'/'+obj.myString)
ECMA-262, 11.2.1:
" The dot notation is explained by the following
syntactic conversion:
MemberExpression . Identifier
is identical in its behaviour to
MemberExpression [ <identifier-string> ]"
In other words, (obj.prop) is identical to (obj['prop'])
which has the value 'defined'.
Try:
alert (obj[prop]+'/'+obj.myString)
|
| Post Reply
|
| Re: Variable names in JSON syntax (Javascript) |
 |
Fri, 20 Jul 2007 16:46:38 +010 |
On Fri, 20 Jul 2007 10:30:52 +0100, Nisse Engström
<news.NOSPAM.gokyoZPGx@luden.se> wrote:
> On Mon, 16 Jul 2007 15:54:08 +0100, Eik wrote:
>
>> Why does the following Javascript display "defined/undefined"
rather
>> than
>> "undefined/defined"?
>>
>> prop='myString'
>> var obj={prop:'defined'}
>> alert(obj.prop+'/'+obj.myString)
>
> ECMA-262, 11.2.1:
>
> " The dot notation is explained by the following
> syntactic conversion:
>
> MemberExpression . Identifier
>
> is identical in its behaviour to
>
> MemberExpression [ <identifier-string> ]"
>
> In other words, (obj.prop) is identical to (obj['prop'])
> which has the value 'defined'.
>
> Try:
>
> alert (obj[prop]+'/'+obj.myString)
I'm talking about the assignment using object notation, not the bracket or
dot notation:
obj={prop:"defined"}
The examples I've seen (eg, http://www.json.org/js.html) usually quote the
property names as well as the values
(obj={"prop":"defined"}), so why does
using a variable name (prop) when defining a property name not result in
that property being called 'myString'? Quotes matter in bracket syntax
(obj[0] is not the same as obj['0']) so I'm just curious why object
notation works differently. Creating associative arrays in PHP (the
equivalent of using object notation in Javascript) requires key names to
be in quotes so that you can distinguish between numbers, strings, and
variable values:
$str='myString';
$arr=array($str => 'As expected!');
echo $arr['myString'];
The equivalent of the above code using Javascript object notation would
display 'undefined' because the property name would end up being 'str'
|
| Post Reply
|
| Re: Variable names in JSON syntax (Javascript) |
 |
Sat, 21 Jul 2007 09:34:01 +020 |
On Sat, 21 Jul 2007 12:55:16 +0800, Andrew Gregory wrote:
>>> On Mon, 16 Jul 2007 15:54:08 +0100, Eik wrote:
>>>
>>>> Why does the following Javascript display
"defined/undefined" rather
>>>> than
>>>> "undefined/defined"?
>>>>
>>>> prop='myString'
>>>> var obj={prop:'defined'}
>>>> alert(obj.prop+'/'+obj.myString)
On Fri, 20 Jul 2007 16:46:38 +0100, Eik wrote:
>> I'm talking about the assignment using object notation, not the bracket
or
>> dot notation:
>>
>> obj={prop:"defined"}
Of course. I was in a hurry yesterday. :-(
> Probably because the prop in the first line is different from the prop in
> the second line, [...
> ...] Javascript is not going to
> evaluate the value of the first prop and use that as the member name of
> the object.
Exactly! The evaluation of prop in obj={prop:'defined'}
is detailed in 11.1.5:
"The production PropertyName: Identifier is evaluated
as follows:
1. Form a string literal containing the same sequence
of characters as the Identifier.
2. Return Result(1)."
I.e., the identifier prop becomes string literal 'prop',
which is returned to be used as the property name.
|
| Post Reply
|
| Re: Variable names in JSON syntax (Javascript) |
 |
Sat, 21 Jul 2007 10:11:11 +020 |
On Mon, 16 Jul 2007 16:54:08 +0200, Eik <spam@hotmail.com> wrote:
> Why does the following Javascript display "defined/undefined"
rather
> than "undefined/defined"?
>
> prop='myString'
> var obj={prop:'defined'}
> alert(obj.prop+'/'+obj.myString)
"defined/undefined" is correct.
Reason:
At the first line, a variable named "prop" is initialised. To this
variable, you assign the string value of 'myString'. This variable is not
used anywhere else in the script.
At the second lie, a new variable named "obj" is initialised. To this
variable, you assign an object having one property named 'prop', which is
assigned the string value of 'defined'. Watch: you are not reusing the
prop variable you set in the first line. You are not setting a property
named 'myString', as you expect. Instead, you create a new property named
'prop'.
At the third line, you query the property created in the second line,
which has the string value of 'defined'. Querying it will return its
string value, 'defined'. You also query a property you didn't define
anywhere, namely the myString property. Because that property isn't
defined, querying it will return 'undefined' or undefined, depending on
your javascript implementation.
To have the query return "undefined/defined", rewrite the code without
instantiation abbreviation, like so:
var prop='myString';
var obj = new Object();
obj[prop] = 'defined';
alert(obj.prop+'/'+obj.myString);
>
> According to any specs/examples I can find, JSON property names should
> be in quotes so wouldn't this mean the unquoted string 'prop' should be
> expanded to its value ('myString'), just like it would using array
> syntax:
>
> obj[prop]='value'
> alert(obj.myString) // Displays 'value'
This unfortunately is not true. JSON properties don't have to be strings.
The non-string way will create properties just the same, and they will be
able to be queried using array syntax either way. The reason to have the
properties as strings as far as I can tell, is to make it easier to
deserialise an JSON object.
--
Sincerely,
ΩJr
Using Opera, Version 9.21, Build 8776
|
| Post Reply
|
|
|