Should I use an empty property key?

前端 未结 5 730
名媛妹妹
名媛妹妹 2020-12-13 08:18

I\'ve tested this only in Firefox, but apparently you can use an empty string as a key to a property in an object. For example, see the first property here:

         


        
5条回答
  •  有刺的猬
    2020-12-13 09:06

    Technically, there is nothing wrong and you can savely use it on any js engine (that I'm aware of). Since ECMAscripts spec says any object key is a string, it of course can also be an empty string.

    The only caveat is, that you'll never be able to access that property with the dot notation

    countsByStatus.;
    

    will lead to a syntax error of course, so it always needs to be

    countsByStatus[''];
    

    That much about the technical part. If we talk about the convinient part, I'd vote for a very clear no, never use it.

    It'll lead to confusion and as we all know, confusion is the enemy.

提交回复
热议问题