Isn't a JavaScript "Object" just a dictionary? Why is there a fancy name for Object Literal Notation? And for JavaScript Object Literal Notation, you can only refer to the object's property's value with Object.property
and not Object[property]
(as you would do in Python)?
Isn't a JavaScript "Object" just a dictionary? Why is there a fancy name for Object Literal Notation? And for JavaScript Object Literal Notation, you can only refer to the object's property's value with Object.property
and not Object[property]
(as you would do in Python)?
-
8
Object[property]
is valid in Javascript. – Ivan Commented Aug 21, 2012 at 23:20 -
If
property
is strictly numeric or includes spaces or other characters or is the result of an expression, you must useObject[property]
– Michael Berkowski Commented Aug 21, 2012 at 23:21 - Everything in JS is a dictionary, but i guess i always thought of object-literal notation as a way to define objects in a more readable way that feels like classes. – pdizz Commented Aug 21, 2012 at 23:25
-
3
@MichaelBerkowski: A Python dict doesn't have to just contain scalars. The values can be anything, including functions and
"I am z"
s. – voithos Commented Aug 21, 2012 at 23:27
1 Answer
Reset to default 8Javascript's "Object Literal Notation" is simply the syntax that javascript accepts for a statically declared data structure. While a javascript object can be used similarly to what is called a dictionary in other languages, "Object Literal Notation" is just a static declaration syntax. It would typically look something like this:
var myObject = {
name: "Peter Foti",
'course': 'JavaScript',
grade: 'A',
level: 3
};
The term "Object Literal Notation" does not refer to all possible ways that you can address an object with javscript code. You don't use either the Object.property
or Object[property]
syntax to define a property in a static object declaration except as the value part of a declaration where the Object is some other object.
This article has a good reference on the Object Literal syntax under the heading Basic Syntax.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742152546a4391846.html
评论列表(0条)