嵌套javascript哈希的默认值 [英] default values for nested javascript hashes

查看:71
本文介绍了嵌套javascript哈希的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaScript中,我想执行以下操作:

In JavaScript I want to do the following:

var pi = {};pi [0] ['*'] ['*'] = 1;

当然,这会引发无法读取未定义的属性'*'"错误.显然,我可以定义p [0] = {},但这有点麻烦,因为我会在不同的属性中粘贴很多不同的值,例如

of course this throws a "Cannot read property '*' of undefined" error. Clearly I can define p[0] = {}, but that's kind of a pain as I will be sticking lots of different values in the different attributes, e.g.

pi [2] ['O'] ['I-GENE'] = 1;

等哈希的第一个键只是一个整数,所以我想我可以在顶层使用数组而不是哈希,然后像本文中那样默认初始化:

etc. The first key into the hash is just an integer, so I guess I could use an array at the top level instead of a hash, and then default initialize like in this post:

默认数组值

但这不能满足我对其他散列的默认初始化的需要.

but that doesn't handle my need for default initialization of the other hashes.

似乎我要执行的操作违反了ECMAScript规范,该规范指示对象的未定义属性(JavaScript中的哈希)应返回未定义,如此处所述:

It does seem like what I am trying to do is running up against the ECMAScript spec which indicates that undefined attributes of an object (which is a hash in JavaScript) should return undefined, as mentioned here:

设置javascript对象属性的默认值

有趣的是,其中包含一个危险的解决方法.

which interestingly includes a dangerous work around.

在其他尝试使用此类嵌套哈希的地方,我发现自己正在编写如下这样的长代码:

In other places where I'm trying use nested hashes like this I am finding myself writing long bits of ugly code like this:

function incrementThreeGramCount(three_grams,category_minus_two,category_minus_one,category){
    if(three_grams[category_minus_two] === undefined){
      three_grams[category_minus_two] = {};
    }
    if(three_grams[category_minus_two][category_minus_one] === undefined){
      three_grams[category_minus_two][category_minus_one] = {};
    }
    if(three_grams[category_minus_two][category_minus_one][category] === undefined){
      three_grams[category_minus_two][category_minus_one][category] = 0;
    }
    three_grams[category_minus_two][category_minus_one][category]++;
}

在这里我真的想避免,或者至少找到一些通过原型方法添加到Hash功能中的好方法.但是,由于JavaScript中的Hash和Object似乎是同一回事,因此我们不能在不影响JavaScript的其他情况的情况下,真正使用JavaScript中的默认哈希行为.

which I'd really like to avoid here, or at least find some good way of adding to the functionality of the Hash through the prototype method. However it seems like since the Hash and Object in JavaScript are just the same thing, we can't really play around with default hash behaviour in JavaScript without impacting a lot of other things ...

也许我应该编写自己的Hash类...或使用原型:

Maybe I should be writing my own Hash class ... or using Prototypes:

http://prototypejs.org/doc/latest/language/Hash/

或其他人

http://www.daveperrett.com/article/2007/07/25/javascript-hash-class/

或mootools:

http://mootools.net/docs/more/Types/Hash

啊,这么多选择-希望我知道这里的最佳做法...

argh, so many choices - wish I knew the best practice here ...

推荐答案

可以使用

This can be done using ES6 proxies. You'd define a proxy on an object with a get handler. When a get is performed on a key with an undefined value or for which the object doesn't have an own property, you set it to a new proxy that uses the same get handler and return that new proxy.

此外,这无需括号语法即可工作:

Additionally, this would work without the need for the bracket syntax:

var obj = ...;
obj.a.b.c = 3;

不幸的是,作为ES6功能,它们的支持仅限于Firefox,并且可以通过带有实验性标记的Chrome在中启用.

Unfortunately, being an ES6 feature, their support is limited to Firefox and they can be enabled in Chrome with an experimental flag.

这篇关于嵌套javascript哈希的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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