4 ms·
Nobody seems to notice this, but using quotes stops you from error of using {foo:"bar"}. In JavaScript, {foo:"bar"} is not an Object, it is a code block contain
by Sephr 17y ago
Nobody seems to notice this, but using quotes stops you from error of using {foo:"bar"}. In JavaScript, {foo:"bar"} is not an Object, it is a code block containing a labeled code block with the label, foo, in it with a string, "bar". This is what {foo:"bar"} really means in JavaScript:
{
foo: {
"bar";
};
}
Labels cannot be strings, so quoting them will help you catch this error earlier.
- aston 17y agoa={foo:"bar"} creates an object called a with foo as a key mapping to "bar".
- tlrobinson 17y agoI'm not sure if your particular example is correct, but you're right that there are certain ambiguous cases involving labels and object literals. There was a really good talk recently that touched on this, but I can't remember who gave it or what it was called.
- axod 17y agoThat's not a good reason. It's simple to spot that and enclose it in brackets.