我应该使用空的属性键吗? [英] Should I use an empty property key?

查看:115
本文介绍了我应该使用空的属性键吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仅在Firefox中对此进行了测试,但显然您可以使用空字符串作为对象中属性的键。例如,请在此处查看第一个属性:

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:

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

在浏览EcmaScript规范时,似乎(至少在5中),属性键被定义为字符串,而字符串被定义为0或更多字符。这意味着根据规范,空字符串是有效的属性名称。

In skimming through the EcmaScript specs, it appears that (at least in 5), property keys are defined as strings, and strings as 0 or more characters. This implies that an empty string is a valid property name according to the specs.

无论如何,我很想在我正在计算的代码段中使用它数据项状态的一些计数摘要(类似于我上面所示)。有些项目可能没有状态,我需要一个占位符。由于状态是用户可定义的,我不想冒险使用可能冲突的虚拟词。

Anyway, I'm tempted to use this in a section of code where I'm calculating summaries of some counts by the status of a data item (similar to what I've shown above). There are some items which might not have a status, and I need a placeholder for those. Since statuses are user-definable, I don't want to risk using a dummy word that might conflict.

看起来如此简单和优雅,在查看数据时我可以很容易地告诉空字符串是什么意思。它还使代码更有效,因为空字符串将是没有状态的项目中状态的确切值。

It seems so simple and elegant, in looking at the data I can easily tell what the blank string would mean. It also makes the code a little bit more efficient, since the empty string would be the exact value of the status in the items without a status.

但与此同时,我的直觉告诉我它出了问题。我的意思是,除了某些浏览器可能不支持这种情况的机会之外,我觉得我在JavaScript中遇到了一个将在某天修复的错误。但是,与此同时,我曾经对我现在每天使用的许多其他JavaScript功能产生了同样的感觉(例如我发现&&和||返回其中一个操作数的值的时间,不只是真或假)。

But at the same time, my instincts are telling me that something is wrong with it. I mean, apart from the chance that some browser might not support this, I feel like I've encountered a bug in JavaScript that will be fixed some day. But, at the same time, that's the same feeling I once had about a lot of other JavaScript features that I now use every day (such as the time I discovered that && and || returns the value of one of the operands, not just true or false).

推荐答案

对象的键必须是字符串,空字符串(''一个字符串。没有交叉浏览器问题,我曾经遇到过空字符串,尽管很少有人认为使用空字符串作为键名是可以接受的。

An object's key must be a string, and the empty string ('') is a string. There is no cross browser issue that I've ever come across with empty strings, although there have been very few occasions where I thought it was acceptable to use an empty string as a key name.

我会阻止 ''的一般用法作为键,但对于简单的查找,它会工作得很好,听起来很合理。这是一个添加注释以注意特殊情况的好地方。

I would discourage the general usage of '' as a key, but for a simple lookup, it'll work just fine, and sounds reasonable. It's a good place to add a comment noting the exceptional circumstance.

此外,在查找期间,您可能会遇到投射到字符串的值的问题:

Additionally, during lookup you may have issues with values that are cast to a 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']

如果你想拥有 null undefined 使用''键,您可能需要使用后备:

If you'd like to have null and undefined use the '' key, you may need to use a fallback:

key = key || '';

如果您传入非字符串值,那么投射也很重要:

If you might have non-string values passed in, it's important to cast too:

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

请注意, 0 的值将变为进入'',而'0'的值将保持'0'

note that a value of 0 will turn into '', whereas a value of '0' will stay '0'.

在大多数情况下,我发现我从中选择了一个预先定义的值 hashtable 对象。要检查对象上是否存在该值,有许多选项:

In most cases, I find I'm picking a pre-defined value out of a hashtable object. To check that the value exists on the object there are a number of 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)) {...}

这篇关于我应该使用空的属性键吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆