javascript关联数组中的属性名称"var"

dr *_*rry 5 javascript safari specifications

出于好奇的快速提问:

下面的代码适用于Firefox和Chrome,但不适用于Safari.这个Javascript规范是否被Firefox和Chrome规避了,或者这是Safari的一个怪癖?

 var a = {};
 a.var = "test";
Run Code Online (Sandbox Code Playgroud)

在所有(火狐,野生动物园和铬)

 a["var"] = "test";
 a.id = "another test";
Run Code Online (Sandbox Code Playgroud)

按预期工作.

干杯,Jeroen.

Thi*_*ter 5

var 是一个保留的关键字,因此在使用时可能会破坏而不会被引用.


Shl*_*emi 3

根据 ecmascript 规范,这是不允许的,

来自 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
Run Code Online (Sandbox Code Playgroud)

基于 Safari 是首选行为