排序图实施 [英] Sorted Map Implementation

查看:51
本文介绍了排序图实施的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务

在我的JavaScript代码中,我经常使用对象将键映射"到值,以便以后可以通过特定值直接访问它们.例如:

In my JavaScript code i'm often using objects to "map" keys to values so i can later access them directly through a certain value. For example:

var helloMap = {};
helloMap.de = "Hallo";
helloMap["en"] = "Hello";
helloMap.es = "Hola";

因此,我使用两种可用的符号 object style array style 在我的源代码中逐步构建了地图对象.

So i build up the map object step by step in my source code using the two available notations object style and array style.

随后,我可以访问例如通过 helloMap ["de"] 添加的值.因此,如果我不必关心在对象上设置属性的顺序,就可以了.

Later i can then access the values i added through helloMap["de"] for example. So thats all fine if i don't have to care about the order in which the attributes has been set on the object.

据我所知,如果我现在想迭代对象的属性,则无法确保以添加对象的顺序对其进行迭代.

If i want to iterate the objects properties now as far as i know there is no way to ensure that i'll iterate them in the order they have been added.

注意:我不能使用某些包装对象,而只是在其中保存一个数组,然后使用其方法添加值,如下所示:

Note: I can't use some wrapper object and simply hold a array in there and then use its methods to add the values so something like this:

var HelloMap = function(){
  this.myMap = [];
  this.addProperty = function(key, value){
    this.myMap.push({key: key, value: value});
  }
}

或类似的内容对我不起作用.因此,该解决方案对于使用该对象的程序员必须是绝对透明的.

or something similar won't work for me. So the solution needs to be absolutely transparent to the programmer using the object.

那表示我需要的对象将是一个空对象,该对象保持添加到其中的属性的顺序.这样的事情会做:

That said the object i needed would be an empty object which maintains the order of the properties that were added to it. Something like this would do:

var helloMap = {};
helloMap = getOrderAwareObject(helloMap);

,以便在对象"中跟踪形式为 helloMap.xy ="foo" helloMap ["yz"] ="bar" 的所有其他分配按顺序",

so that every further assignment of the form helloMap.xy = "foo" and helloMap["yz"] = "bar" would be tracked in the object "in order",

可能的解决方案

由于我没有在下划线或jQuery中找到任何解决方案,因此给我提供了这样一个特殊的对象,因此我遇到了使用 Object.defineProperty 在JavaScript对象中定义属性的getter和setter方法的可能性,因为我可以依靠在 ECMAScript 5 标准上,我可以使用它.

Since i did not find any solution in underscore or jQuery giving me such a special object i came across the possibility of defining getters and setters for properties in JavaScript objects with Object.defineProperty since i can rely on ECMAScript 5 standard i can use it.

这个问题是,在实际设置属性之前,您必须知道可以在该对象上设置的所有可能的属性.因为如果您定义它,您必须命名它.

The Problem with this one is, that you have to know all the possible properties that can be set on the object, before they are actually set. Since if you define it you got to name it.

我要搜索的是类似 Default Getter Default Setter 的东西,如果没有为属性定义getter和setter的话,它们将应用于对象.这样,我就可以将排序后的地图隐藏在对象界面的后面.

What i am searching for is something like a Default Getter and Default Setter which applies on the object if no getter and setter has been defined for the property. So i could then hide the sorted map behind the object inteface.

  • 您知道的任何框架中都已经有解决方案了吗?
  • 是否有类似默认获取器/设置器"的机制?

推荐答案

恐怕您将需要在内部使用数组进行某种包装.ECMAScript 5(这是当前浏览器JavaScript实现所基于的标准)完全不允许有序对象属性.

You'll need a wrapper of some kind using an array internally, I'm afraid. ECMAScript 5 (which is the standard on which current browser JavaScript implementations are based) simply doesn't allow for ordered object properties.

但是,ECMAScript 6将具有 Map 实现,该属性具有有序的属性.另请参见 http://www.nczonline.net/blog/2012/10/09/ecmascript-6-collections-part-2-maps/.

However, ECMAScript 6 will have a Map implementation that has ordered properties. See also http://www.nczonline.net/blog/2012/10/09/ecmascript-6-collections-part-2-maps/.

ECMAScript 6中可能还存在其他选项.请参见以下问题:

There may also be other options in ECMAScript 6. See the following question:

如何使用ECMAScript 5定义默认的getter和setter?

这篇关于排序图实施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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