Using reserved words as property names, revisited

后端 未结 4 1280
臣服心动
臣服心动 2020-11-28 09:27

Can a reserved word be used as an object\'s property name?

This issue was raised indirectly in a previous Stack Overflow question: Browser support for using a reserv

4条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-28 10:04

    Yes, it can be used.

    Just small remark, if you use YUI compressor you have to put property name which is equal to one of js reserved words in quotes.

    For example, this won't compress

    var a = { case : "foo"}; // syntax error, "invalid property id"
    a.for = "bar"; // syntax error, "missing name after . operator"
    

    This will do

    var a = { "case" : "foo"}; //OK
    a["for"] = "bar"; //OK
    

    Here is Online JavaScript/CSS Compression Using YUI Compressor where this can be tested.

提交回复
热议问题