Should I use an empty property key?

前端 未结 5 731
名媛妹妹
名媛妹妹 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:04

    The problem is that since the statuses are user-defineable there is nothing stoping the user from also using the empty string as a status, thus ruining your logic. From this point of view what you are doing is no different then just using an ugly custom name like __$$unknown_status. (Well, I'd say the ugly custom name is more descriptive but to each its own...)

    If you want to be really sure the "unknown" property does not collide you need to keep it separate:

    var counts = {
        unknownStatus: 23,
        byStatus: {
            "": 17, //actual status with no name, (if this makes sense)
            "started": 45,
            "draft": 3,
            "accepted": 23,
            "hold": 2345,
            "fixed": 2,
            "published": 345
        }
    };
    

提交回复
热议问题