Should I use a property key blank?

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

var countsByStatus = { "": 23, //unknown status "started": 45, "draft": 3, "accepted": 23, "hold": 2345, "fixed": 2, "published": 345 } 

When viewing EcmaScript specifications, it appears (at least in 5), property keys are defined as strings, and strings are 0 or more characters. This means that the empty string is a valid property name according to the specifications.

In any case, I am tempted to use this in the code section, where I calculate a summary of some values ​​by the status of the data item (similar to what I showed above). There are some elements that may not have status, and I need a placeholder for them. Since statuses are user-defined, I don’t want to risk using a dummy word that could conflict.

It seems so simple and elegant, looking at the data, I can easily say what an empty string would mean. It also makes the code a bit more efficient, since an empty string will be the exact status value in elements with no status.

But at the same time, my instincts tell me that something is wrong with this. I mean, in addition to the fact that some browsers may not support this, I feel that I have encountered a bug in JavaScript that will be fixed once. But at the same time, it feels like I once had many other JavaScript functions that I use every day now (for example, when I found that && and || returns the value of one of the operands, and not just true or false).

+56
javascript
Dec 01 '11 at 15:34
source share
5 answers

The object key must be a string, and an empty string ( '' ) is a string. There is no cross-browser problem that I have ever encountered with empty strings, although there were very few cases where I considered it acceptable to use an empty string as the key name.

I would refuse the general use of '' as a key, but for a simple search, it will work fine and sounds reasonable. This is a good place to add a comment, noting an exceptional circumstance.

In addition, during the search, you may have problems with the values ​​that are passed to the string:

 o = {...} //some object foo = 'bar'; //some examples o[foo] //will return o['bar'] o[null] //will return o['null'] o[undefined] //will return o['undefined'] 

If you want null and undefined use the '' key, you may need to back up:

 key = key || ''; 

If you may have non-string values, they are also important:

 key = key || ''; key = '' + key; 

note that a value of 0 will turn into '' , while a value of '0' will remain '0' .




In most cases, I find that I select a predefined value from an object hashtable . To check if a value exists for an object, there are several options:

 //will be falsey if the value is falsey if (o[key]) {...} //will return true for properties on the object as well as in the prototype hierarchy if (key in o) {...} //returns true only for properties on the object instance if (o.hasOwnProperty(key)) {...} 
+18
Dec 01 '11 at 20:21
source share

Technically, there is nothing wrong, and you can use it on any js engine (which I know about). Since the ECMAscripts specification says that any object key is a string, it can of course also be an empty string.

The only caveat is that you can never access this property using dot notation.

 countsByStatus.; 

will result in a syntax error, of course, so there should always be

 countsByStatus['']; 

Here is the technical part. If we talk about the convenient part, I will vote for a very clear not , never use it.

This will lead to confusion, and as we all know, confusion is the enemy.

+11
Dec 01 '11 at 15:44
source share

The problem is that since the statuses are user-defined, nothing prevents the user from using an empty string as status, thereby destroying your logic. From this point of view, what you are doing is no different, just using an ugly custom name like __$$unknown_status . (Well, I would say that the ugly user name is more visual, but for each of them ...)

If you want to be sure that the unknown property does not collide, you need to save it separately:

 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 } }; 
+3
Dec 01 '11 at 15:47
source share

I think everything is in order. "" has semantics in your application and its actual javascript. So on it.

note that

x."" = 2;

an error will come out, so you need to use syntax like

x[""] = 2;

+2
Dec 01 '11 at 15:44
source share

Is "unknown status" a null value or is your status field "non-zero"?

In the first case, I would say that you will need to use a separate counter, in the second I would say that "empty" is a completely valid status - just use the word "unknown" to display instead of "This can only lead to confusion when your the user uses the same word as the state type, but to prevent that you can use a different visual style to display the text "unknown state".

0
Dec 01 '11 at 16:14
source share



All Articles