Javascript Object - Key beginning with number, allowed?

前端 未结 4 1603
庸人自扰
庸人自扰 2020-12-06 09:22

Is this allowed?

myObj = {};
myObj[\'4a56546s6d\']

Or the key must start with a letter like:

myObj = {};
myObj[\'x4a56546s6         


        
4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-06 09:47

    See this page: https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Core_Language_Features#Variables

    A JavaScript identifier must start with a letter, underscore (_), or dollar sign ($); subsequent characters can also be digits (0-9). Because JavaScript is case sensitive, letters include the characters "A" through "Z" (uppercase) and the characters "a" through "z" (lowercase).

    You can use it that way, but you won't be able to access the data by using myObj.4a56546s6d because starting an identifier with a number is not allowed.

提交回复
热议问题