我正在构建我的一个小项目,并且没有找到其他解决方案来保证具有迭代顺序的键值关系。
到目前为止我想出的是这个,
var Plate = function () {
this.children = [];
this.childrenKeys = [];
};
var proto = Plate.prototype;
proto.addChild = function (key, child) {
this.childrenKeys.push(key);
this.children.push(child);
};
proto.removeChild = function (key) {
var index = this.childrenKeys.indexOf(key);
delete(this.children[index]);
};
proto.update = function () {
this.children.forEach(function (child) {
child.update();
});
};
虽然这确实有效,我可以Plate
用一个键和一个值添加,我可以用一个键从盘子中删除,但这样做似乎不正确。
我知道我不能使用一个对象,for...in
因为迭代的顺序是完全不一致的,我不能让它像那样。
这是执行此操作的正确方法,还是有内置的 javascript 方法来执行此操作?
谢谢