6

From Javascript: The Definitive Guide,

var o = { x:1 }; // Start with an object
o.x = 2; // Mutate it by changing the value of a property
o.y = 3; // Mutate it again by adding a new property

What does { x: 1} do here? With the braces, it reminds me of function (or for objects, constructor). Can someone please elaborate on it, thanks.

An additional related question is:

({x:1, y:2}).toString() // => "[object Object]"

I find this question interesting as well. What is the difference between object and Object in the above code? In fact, when do we use Object?

CppLearner
  • 16,273
  • 32
  • 108
  • 163

4 Answers4

11

It makes the variable o into an object containing one property (x), and sets the value of that property to 1.

Edit

To be clear, as you demonstrated, you don't have to add properties this way. You can create a property on an object simply by assigning it (o.y = "Awesomesauce")

As to your related question; {x:1, y:2} is simply an object literal with two properties x and y with the values 1 and 2, respectively. operating on this object literal is just like operating on a primitive value literal (console.log("my,string".split(","))).

"[object Object]" is just how a non-specifically typed object is represented in string form.

Edit 2

As per your comment: the lowercase "object" is the type. typeof o will give object. The Object (capital "O") is just a string representation of {x:1}. A string representation of an array or number is "smart" because it knows more specifically the type. With a custom object, like o, it's just a generic object and thus writes: object (the type) Object (a string representation of o itself)

Community
  • 1
  • 1
Thomas Shields
  • 8,874
  • 5
  • 42
  • 77
  • Sheidls Thanks. Very good answer. What now confuses me is "object vs Object". x being the property is object? With the capitalization, Object, how is it different from object? Thanks! – CppLearner Aug 07 '11 at 23:00
  • Actually. the object is {x:1}, and Object is the 1, right? Since Number(1) returns object, Object(1) returns object (using typeof)... kinda confuse here. – CppLearner Aug 07 '11 at 23:09
4

It's called object initializer (At least in C#). It creates a new object o and directly initiates the attribute x with the value 1

Sandro
  • 2,998
  • 2
  • 25
  • 51
  • 3
    +1, Not only in C#, in ECMAScript also, they are called [Object Initialiser Expressions](http://es5.github.com/#x11.1.5) (or just *"object literals"*) :). – Christian C. Salvadó Aug 07 '11 at 01:01
3

It's a JSON object, an associative array. The key is "x"; the value is 1.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • 9
    Don't confuse the object literal syntax, with the JSON *data interchange format*, they have [differences](http://stackoverflow.com/questions/3975859/what-are-the-differences-between-json-and-javascript-object). – Christian C. Salvadó Aug 07 '11 at 00:57
  • 5
    +1 for @CMS. It's really just a JavaScript object. Crockford introduced the term JSON long, long, long after JavaScript was created. Anyway, the string `{x:1}` is completely illegal in JSON. JSON requires keys to be double quoted. Check the official syntax: http://www.json.org. – Ray Toal Aug 07 '11 at 01:05
2

You already have answers to the first part of your question.

For the second part, you have called the toString() method stored in Object.prototype.

For fun, try this instead:

JSON.stringify({x:1, y:2})
Ray Toal
  • 86,166
  • 18
  • 182
  • 232