为参数创建函数映射的最有效方法是什么? [英] What is the most efficient way to create map of functions to arguments?

查看:68
本文介绍了为参数创建函数映射的最有效方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为其参数创建一个函数映射。此地图将动态更新。最后,将使用相应的参数调用所有函数。

I want to create a map of functions to its argument. This map will be updated dynamically. Finally all the functions will be called with their corresponding arguments.

function foo1(x) { 
    //do something with x
}

function foo2(x) { 
    //do something else with x
}

var map = {};
map[foo1] = [1,2,3];  //this array is updated dynamically in my code
map[foo2] = [4,5,6];

// I want to call foo1 and foo2 with their [1,2,3] and [4,5,6] arguments respectively.

我尝试了2种方法:


  • foo1 转换为字符串(使用 toString()方法)作为地图的关键字。然后我使用Function构造函数从该字符串返回函数。但我担心这会影响性能。

  • Converted foo1 to string (using toString() method) as the key for the map. Then later I get back the function from this string using Function constructor. But I am afraid if this will hit the performance.
// This works. But concerned about the performance

map[foo1.toString()] = [1,2,3];

for(i in map){
    var fn = Function( 'return '+ i)();
    fn(map[i]);
}




  • 存储包裹功能的对象及其各自的对象参数如:

  •    { fn : foo1 , args : [1,2,3] }
       { fn : foo2 , args : [4,5,6] }
    

    这里我存储了对函数的引用而不是整个函数定义。但我必须遍历整个数组以添加更多参数。

    Here I store the references to functions instead of the entire function definition. But I have to traverse through the entire array to add more arguments.

    有没有更好的方法来维护这个地图?上述方法有哪些缺点?

    Is there any better approach to maintain this map? What are the drawbacks in the above mentioned approaches?

    更新

    回答问题在什么情况下我需要这个:

    Answer to the question "in what situation I will need this" :

    我正在维护从参数到函数的映射。我动态更新它。
    稍后在我的代码中我想创建一个反向映射并用其所有参数调用函数。

    I am maintaining a map from arguments to functions. I update it dynamically. Later in my code I want to create a reverse map and call the functions with all its arguments.

    例如:

    1 -> foo1
    2 -> foo2
    3 -> foo1,foo2
    4 -> foo1
    ... and so on.
    

    然后我想创建一个这样的反向地图:

    Then I want to create a reverse map like this :

    foo1 -> [1,3,4...]
    foo2 -> [2,3,...]
    

    最后致电:

    foo1( [1,3,4...])
    
    foo2( [2,3,...])
    


    推荐答案

    直到有一个原生的跨浏览器解决方案让对象成为密钥,您可以随时实施自己的解决方案。这是你可以做的一个例子。在下面的代码中, ObjectMap 将生成的密钥存储为对象的属性 需要作为属性用于在对象上存储的名称随机化以减少可能的冲突。然后,地图实现可以使用此属性来检索对象上,然后检索它的关联

    Until there's a native cross-browser solution for having objects as keys, you could always implement your own solution. Here's an example of what you could do. In the code below, the ObjectMap will store a generated key as a property of the object that needs to serve as a key. The property name that is used to store the key on the object is randomized to reduce possible conflicts. The map implementation can then use this property's value to retrieve the key on the object and then retrieve it's associated value.

    JSPERF: http://jsperf.com/object-map

    JSPERF: http://jsperf.com/object-map

    function ObjectMap() {
        this.key = 0;
        //you should implement a better unique id algorithm
        this.mapId = '_' + Math.floor(Math.random() * 10000);
        this.data = {};
    }
    
    ObjectMap.prototype = {
        set: function (object, value) {
            var key = ++this.key;
    
            if (object[this.mapId]) {
                return;
            }
    
            object[this.mapId] = key;
            this.data[key] = value;
        },
    
        get: function (object) {
            var key = object[this.mapId];
    
            return key? this.data[key] : null;
        },
    
        remove: function (object) {
            var key = object[this.mapId];
            if (!key) {
                return;
            }
            delete this.data[key];
            delete object[key];
        }
    };
    
    function a() {}
    
    var map = new ObjectMap();
    
    map.set(a, 'test');
    
    console.log(map.get(a)); //test
    

    这篇关于为参数创建函数映射的最有效方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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