Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does {x:1} do in Javascript?

Tags:

javascript

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?

like image 854
CppLearner Avatar asked Aug 07 '11 00:08

CppLearner


People also ask

What does += mean in JS?

The addition assignment operator ( += ) adds the value of the right operand to a variable and assigns the result to the variable. The types of the two operands determine the behavior of the addition assignment operator.

What does X mean in JavaScript?

var is the keyword that tells JavaScript you're declaring a variable. x is the name of that variable. = is the operator that tells JavaScript a value is coming up next. 100 is the value for the variable to store.

What is $() in JavaScript?

$() The $() function is shorthand for the getElementByID method, which, as noted above, returns the ID of a specific element of an HTML DOM. It's frequently used for manipulating elements in a document. $() allows for shorter and more efficient JavaScript coding. Traditional method: document.

What is the meaning of 1 in JavaScript?

NOT (!): toggles a statement from true to false or from false to true. ! 0 = true ! 1 = false. This is a brilliant introduction to boolean operators and their use in javascript.


2 Answers

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)

like image 142
Thomas Shields Avatar answered Oct 21 '22 05:10

Thomas Shields


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

like image 27
Sandro Avatar answered Oct 21 '22 03:10

Sandro