最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

In JavaScript, isn't Object Literal Notation just another name for a dictionary? - Stack Overflow

programmeradmin12浏览0评论

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)?

Share Improve this question asked Aug 21, 2012 at 23:18 bigpotatobigpotato 27.6k60 gold badges194 silver badges360 bronze badges 4
  • 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 use Object[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
Add a ment  | 

1 Answer 1

Reset to default 8

Javascript'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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论