Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

attribute name "var" in javascript associative array

Quick question out of curiosity:

code below works in Firefox and Chrome but not in Safari. Is this Javascript spec circumvented by Firefox and Chrome or is this a quirk 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.

like image 859
dr jerry Avatar asked Jun 13 '11 08:06

dr jerry


2 Answers

var is a reserved keyword so it might break when being used without being quoted.

like image 163
ThiefMaster Avatar answered Sep 28 '22 06:09

ThiefMaster


according to the ecmascript specification its 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 that safari is the preferred behavior

like image 25
Shlomi Komemi Avatar answered Sep 28 '22 08:09

Shlomi Komemi