Why does this object literal cause a syntax error? [duplicate] - javascript

This question already has answers here:
JavaScript object literals syntax error
(2 answers)
Closed 7 years ago.
I´m kind of confused as to why I can assign the following object literal:
var x = { "a" : true };
While changing it to a simple statement:
{ "a" : true };
Will cause SyntaxError: Unexpected token : in Firefox and Chrome (it works as expected in Node).

JavaScript object literals syntax error
Here is a similar answer (assuming you used the REPL). Basically Node will wrap the code in parentheses, thus parsing it successfully as an object literal.
{"hello":1} to be ({"hello":1})

Related

you might need to wrap the the whole assignment in parentheses [duplicate]

This question already has answers here:
What is this javascript syntax where you have () around the whole variable expression?
(2 answers)
How to destructure an object to an already defined variable? [duplicate]
(1 answer)
Closed 6 months ago.
when run in console:
let b;
{b} = {b:1} // run without error
{b} = {b:1} // empty line before this code, and run with error VM243:2 Uncaught SyntaxError: Unexpected token '='
({b} = {b:1}) // run without error
when run in https://www.typescriptlang.org/play, error message like below:
Declaration or statement expected. This '=' follows a block of statements, so if you intended to write a destructuring assignment, you might need to wrap the the whole assignment in parentheses.

When I try to use ``` ...``` decorator doesn't work [duplicate]

This question already has answers here:
ECMAScript 6 arrow function that returns an object
(6 answers)
Closed 3 years ago.
I am trying to do this :
setMyState(prevState=> {...prevState, name: e.nativeEvent.text });
While the console says src/components/addItem.js: Unexpected token
And it doesn't work :(
While using a js file .. I tried with .jsx too and same error :(.
Also I found an answer here WebStorm error: expression statement is not assignment or call
but it didn't solve my problem since when I run the app now it crashes exactly there ...
If you use an arrow function and want to return an object you need to wrap your object with (). If you don't use, arrow function thinks {} is the body block. So, try using:
setMyState(prevState=> ({...prevState, name: e.nativeEvent.text }));

Why property accessor does not work for an object literal [duplicate]

This question already has answers here:
What difference does it makes to parse something as an object literal rather than as a block?
(2 answers)
Why doesn't `{}.toString.apply(array)` work?
(1 answer)
Closed 5 years ago.
When I create an object, assign it to a variable, then everything works:
var obj = {"a": 1};
obj.a; // 1
But when I remove the variable, it does not work anymore:
{"a": 1}.a // Uncaught SyntaxError: Unexpected token :
And if I enclose the object literal in parentheses, it works again:
({"a": 1}).a // 1
I think the difference is that the object literal gets interpreted as a block, and the interpreter tries to execute the "a": 1 part, which is obviously wrong. Adding parentheses forces the object creation.
Am I on the right track? What are the rules whether something is considered a block vs an object?

How do I correctly include a dash in an object key in javascript? [duplicate]

This question already has answers here:
How can I access object properties containing special characters?
(2 answers)
Closed 1 year ago.
When I try to use a code like this:
jQuery.post("http://mywebsite.com/", {
array-key: "hello"
});
I get this error:
Uncaught SyntaxError: Unexpected token -
I tried surrounding the key in quotation marks, but nothing happened. I'd like to know if it's because of some other mistake I am making at the other website or because that's not how to include a dash in an object's key. And if it's not I'd like to know how to do it. Thanks in advance.
jQuery.post("http://mywebsite.com/", {"array-key": "hello"});
should be correct. If this doesn't work, the issue is not with your request.

Getting object properties that start with a number [duplicate]

This question already has answers here:
Can I get a javascript object property name that starts with a number?
(2 answers)
Closed 8 years ago.
I've got an object where one of the properties starts with a number. I've read that this was allowed, but I'm getting this error:
SyntaxError: identifier starts immediately after numeric literal
var stats = {"LOAD_AVG":{"1_MIN":0.08,"5_MIN":0.08,"15_MIN":0.08},"COUNTS":{"TOTAL":888,"RUNNING":1,"SLEEPING":887,"STOPPED":0,"ZOMBIE":0},"CPU":{"USER_CPU_TIME":8.9,"SYSTEM_CPU_TIME":2.4,"NICE_CPU_TIME":0,"IO_WAIT_TIME":1,"HARD_TIME":0,"SOFT_TIME":0.1,"STEAL_TIME":0},"MEMORY":{"PHYSICAL":{"TOTAL":"3921.98mb","IN_USE":"3682.652mb (93.9%)","AVAILABLE":"239.328mb (6.1%)","BUFFERS":"266.492mb (6.8%)"},"SWAP":{"TOTAL":"4194.296mb","IN_USE":"64.264mb (1.5%)","AVAILABLE":"4130.032mb (98.5%)","CACHE":"1191.328mb (28.4%)"}}};
//works fine
window.alert(stats.COUNTS.TOTAL);
//doesn't work
window.alert(stats.LOAD_AVG.1_MIN);
Here's a fiddle.
How can I access the properties that begin with a number without rewriting the PHP that generated it?
You can use bracket access for properties that aren't valid JavaScript identifiers, that goes for property names with spaces or other language symbols like +, *
window.alert(stats.LOAD_AVG["1_MIN"]);
You can use bracket access anywhere really
window.alert(stats["COUNTS"]["TOTAL"]);

Categories

Resources