Attribute name "var" in javascript associative array

Quick question out of curiosity:

The code below works in Firefox and Chrome, but not in Safari. Did this Javascript specification bypass Firefox and Chrome or is it a fad in Safari?

 var a = {};
 a.var = "test";

in all (firefox, safari and chrome)

 a["var"] = "test";
 a.id = "another test";

works as expected.

Cheers, Jeroen.

+5
source share
3 answers

according to the specification of the extent, it is not allowed,

from ecmascript-262: http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf

7.6.1.1 Keywords
The following tokens are ECMAScript keywords and may not be used as Identifiers in ECMAScript programs.

break, do, instanceof, typeof, case, else, new, var, catch, finally, return, void, continue, for, switch, while, debugger, function, this, with, default, if, throw, delete, in, try

based on this safari is the preferred behavior

+3
source

var - , .

+5

It seems that Chrome and Firefox clearly distinguish identifiers (variables, objects, functions, etc.) and object properties. I tried with all reserved words and, as an object, they "work":

var myObject = { new : 30, var: 20 }

You can access each property, and SyntaxError will not be selected.

+1
source

All Articles