user. and number later [duplicate] - javascript

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"]);

Related

convert the literal value with parentheses [duplicate]

This question already has answers here:
Calling member function of number literal
(3 answers)
Closed 3 years ago.
I'm new to Javascript, and I saw code like this:
var myData1 = (5).toString() + String(5);
and the author says he placed the numeric value in parentheses and then called the toString method. This is because you have to allow JavaScript to convert the literal value into a number before you can call the methods that the number type defines.
I'm confused, isn't that 5 is already a number, why 5 needs be converted as (5) to be a number?
The author is partly right. This has nothing todo with turning the literal into a number, this is just about a syntactical distinction: The. can either be used to express fractional numbers (1.1) or it can be used for property access (obj.prop). Now if you'd do:
1.toString()
that would be a syntax error, as the dot is treated as a number seperator. You could do one of the following to use the property access dot instead:
1.0.toString() // as the first dot is the number seperator already, the second dot must be property access
1..toString() // same here
(1).toString() // the dot is clearly not part of the number literal

eval() function in java script throwing error with direct value [duplicate]

This question already has answers here:
Why can't I access a property of an integer with a single dot?
(5 answers)
Closed 4 years ago.
I tried to evaluate string function toString() in console.
In one scenario it is working fine and in another scenario it is not working as expected.
Scenario 1:
eval(99.toString());
output:
Invalid or unexpected token ...
Scenario 2:
var a = 99;
eval(a.toString());
Output:
99
Please help me to understand the difference between both the scenarios.
That has nothing to do with eval.
The error is produced by 99.toString. The reason is that 99. is read as a number (equivalent to 99.0) and then toString is just a random word that doesn't fit the syntax:
99.0 toString // what the parser sees
To fix it, you need to keep . from being treated as part of the number. For example:
99 .toString() // numbers can't contain spaces, so '99' and '.' are read separately
(99).toString() // the ')' token prevents '.' from being read as part of the number
99.0.toString() // '99.0' is read as a number, then '.toString' is the property access
99..toString() // same as above, just with '99.' as the number
99['toString']() // using [ ] for property access, no '.' at all
A numeric literall (99) is not and object with properties. A variable with value 99 like var x = 99 is and object and you can use methods like x.toString()
eval expects a script input (string), in example:
var x = eval('var a = 99; a.toString()');
console.log(x);

Call method directly on number [duplicate]

This question already has answers here:
Why can't I access a property of an integer with a single dot?
(5 answers)
Closed 4 years ago.
I recently implemented a very simple method to extend the base JavaScript Number Class and tried to call it directly on an entered number in the browser console.
123.myMethod();
But it is not working as expected, it only says: "Invalid or unexpected token"
I was unsure, if I can call Number Methods directly on entered numbers, so I tried standard methods like .toFixed():
123.toFixed(1);
But this also isn't working.
Only if I write a float, I can call Number methods:
123.0.toFixed(1);
It also works, if I put the Integer inside brackets:
(123).toFixed(1);
So my question is:
Why are Integers not implicitly casted to Number and why can't I use Number methods on them?
The . tells the JavaScript interpreter to be a decimal point, so it is expecting more numbers. In JavaScript there is only floats, no integers.

SyntaxError: Number.toString() in Javascript [duplicate]

This question already has answers here:
Calling member function of number literal
(3 answers)
Closed 6 years ago.
Why do I get an error on this, in Javascript:
10000000.toString();
You can see an example in here:
http://jsbin.com/kagijayecu/1/edit?js,console
It's because the JS parser is expecting more digits after the "." rather than a method name, e.g. 1000000.0, and in fact 1000000.0.toString() will work as expected.
wrap it inside () like this (10000000).toString()
When JS parse meets dot after digit it expects floating-point literal, e.g. 1000000.0

How does JavaScript interpret indexing array with array? [duplicate]

This question already has answers here:
Why does [5,6,8,7][1,2] = 8 in JavaScript?
(3 answers)
Closed 7 years ago.
[1,2,4,8][0,1,2,3]
// equals to 8 (the last element of the indexing array (3) becomes the index)
Why is this not a SyntaxError error (a bad legacy or a purposeful feature)? (A possible duplicate, however I wasn't able to find an answer here.)
Update: Why the contents of the square brackets are treated as an expression?
The first part:
[1,2,4,8]
is interpreted as an array literal. The second part:
[0,1,2,3]
is interpreted as square bracket notation to access a member of the array. The contents of the square brackets are treated as an expression, which is seen as a sequence of comma separated values:
0,1,2,3 // or (0,1,2,3) as an independent expression
That expression returns the last value, so is effectively:
[1,2,4,8][3] // 8

Categories

Resources