在javascript中排序键/值对集合的最佳实践
Best practice for ordering a collection of key/value pairs in javascript?
在javascript/jQuery中实现以下功能的有效(且用户友好)方法是什么?
根据文件的mime类型,应该执行回调。回调由开发人员通过提供带有可选通配符(例如text/javascript、text/*、*/*)的mime类型描述和回调本身来定义。声明的类型必须按照指定的顺序与文件的给定mime类型匹配。例如,text/plain的优先级必须高于text/*。
这里有一些想法:
使用简单对象
var handler = {
'text/javascript' : callback0,
'text/*' : callback1
}
此将是最直观的解决方案,但订单不能保证。
维护两个列表
var order = ['text/javascript', 'text/*'];
var handler = [callback0, callback1];
This is would be hard to maintain if there are more than two or three types and you are using anonymous functions as callbacks.
Adding an index by wrapping the callback into an object
var handler = {
'text/javascript' : {index: 0, callback0},
'text/*' : {index: 1, callback1}
};
... change thousands of index-properties when inserting an item at the beginning.
Using an array of arrays
var handler = [
['text/javascript', callback0],
['text/*', callback1]
];
This might me more userfriendly than the others, but there is no direct access for known mime-types without iterating over the elements (this would be nice-to-have).
So there are some ways to do the thing I want, but what would be the right way (and why)? Maybe someone has a pattern?
Best, Hacksteak
I would use the 'simple object' solution AND the order array form the 'maintain two lists' solution.
Iterate through the order array with a block of code that uses the handler
simple object to do something that either breaks the loop or continues to the next loop iteration.
EDIT TO RESPOND TO A COMMENT
I agree with your comment. I would do something like this to make it just one variable:
var handlers = {
handler: {
'text/javascript': callback0,
'text/*': callback1
},
order: ["text/javascript", "text/*"]
};
尽管我会为handlers
变量和/或handlers.handler
属性选择更好的名称。
另一条评论的回应
也许你应该修改handlers.handler
和handlers.order
一次一个模拟类型:
var handlers = { handler: {}, order: [] }; // initialize as empty
// add 'text/javascript' mime type
handlers['text/javascript'] = callback0;
handlers.order.push('text/javascript');
// add 'text/*' mime type
handlers['text/*'] = callback1;
handlers.order.push('text/*');
这种方法有点重复,但将来应该很容易维护。如果需要,可以编写一个函数,向handlers.handler
添加一个新属性,并向handlers.order
附加一个新的mime类型。
在大多数供应商中,对象的排序似乎都是按照插入顺序进行的,除了V8,这是真的,除了数字索引。请参阅以下参考资料。
我可能不会做任何循环。我可能会这样做:
var handler = {
text: {
javascript: callback0,
'*': callback1
},
'*' : {
'*': callback3
}
};
var mime = 'whatevs/whatevs'.split('/');
var callback = handler[mime[0]][mime[1]] || handler['*']['*'];
callback();
参考文献
- 使用for循环JavaScript对象属性和数组。。在订购很重要的时候
- http://code.google.com/p/v8/issues/detail?id=164
- 如何使jQuery插件函数可调用以供独立使用,而不在集合上操作
- Meteor-将选定窗体中的对象添加到集合中
- 在localhost Dev Box上测试JSONP请求的最佳方式
- 有条件更新d3.js力图中节点的最佳方法
- 删除集合中旧邮件/帖子的最佳方式
- 在主干中呈现集合的最佳方法
- BackboneJS重新排列集合中模型的最佳方式,同时为每个模型维护0索引的有序属性
- 主干 + 木偶 - 模型/集合的内存管理最佳实践
- 当您有集合时,调用接收单个对象作为参数的函数的最佳方法是什么
- 在集合中移动主干模型的最佳实践
- 正在从集合[性能/最佳实践/问题]中的数组中删除对象
- Backbone.js中混合集合的最佳设计模式
- 将Class实例添加到集合的最佳方法
- Javascript/NodeJS:通过对象数组/集合中的id查找特定对象的最佳方法
- 在Backbone中保存对全局集合的引用的最佳模式
- 给定一个id列表,查询集合中不存在哪些id的最佳方法是什么?
- 为Backbone.js集合触发过滤器的最佳方式
- 为类别集合创建分页订阅的最佳方式是什么
- 在javascript中排序键/值对集合的最佳实践
- 在集合中查找项的第一次出现并更新它的最佳方法是什么?