Do reserved words need to be quoted when set as property names of JavaScript objects?

后端 未结 3 497
北荒
北荒 2020-12-10 02:25

Given an object literal, or jQuery(html, attributes) object, does any specification state that reserved words, or future reserved words MUST be quoted?

Or, can, for

3条回答
  •  没有蜡笔的小新
    2020-12-10 03:00

    This answer cannot compete with those already given but I'd love to chime in nonetheless.

    In my code I prefer to ALWAYS quote keys, for example:

    var o;
    
    o = {
      "label": "Hello",
      "index": 3
    };
    

    This way, the problem of strange names or reserved keywords doesn't even arise. Furthermore, all object literals are written in a style that is very near to valid JSON, as an added bonus copy+paste into a separate JSON file (and vice-versa) can be done very quickly.

    Today, I consider this a must-have style for clean code.

提交回复
热议问题