user796443
user796443

Reputation:

Difference between js objects constructed in different ways

What is the difference between js objects created in this way? :

var js = {};
js.first = "blah";
js.second = something;

vs:

var js {
first : "blah",
second: something
}

In chrome inspector I don't see any problem. I've problem when passing js variable (first example) to socket.emit which gives me empty object in first case but works fine in the second example.

I'm confused.

Reference: https://stackoverflow.com/questions/20828860/why-cannot-i-pass-object-via-node-js-emit

Upvotes: 0

Views: 38

Answers (1)

Sergei Zahharenko
Sergei Zahharenko

Reputation: 1524

There is absolutely no difference between these 2 ways of creating an new object.

First example just shows how dynamically you can add new keys and values to existing object.

if you try to compare them with == you will get false, but even if you create 2 objects similar way, you will get false as well...

var js = {
first : "blah",
second: something
}

var js2 = {
first : "blah",
second: something
}

js == js2 //false

so it seems to be some browser/node bug, if it's giving you empty object. Maybe parser bug? hard to say. But there is no actual difference

Upvotes: 1

Related Questions