Javascript:重用有界函数

时间:2016-02-27 19:08:33

标签: javascript functional-programming

标准Function.prototype.bind方法在每次调用时创建一个新函数。 我需要在某处存储并重用该函数的有界变体。

实现适用于对象参数的函数bindArg并非火箭科学:

const fnStorage = new WeakMap();
function bindArg(fn, arg) {
  if (!fnStorage.has(fn)) {
    fnStorage.set(fn, new WeakMap());
  }
  if (!fnStorage.get(fn).has(arg)) {
    fnStorage.get(fn).set(arg, fn.bind(null, arg));
  }
  return fnStorage.get(fn).get(arg);
}

此解决方案工作正常,但仅适用于对象参数。通过将WeakMap更改为Map,可以为标量参数执行此操作。但这并不好,因为Map将继续引用函数的有界变体并防止垃圾收集它。

有没有办法实现bindArg函数,它是纯不可变的,没有内存泄漏任何类型的参数?

1 个答案:

答案 0 :(得分:2)

您可以使用Map表示原始数据类型,使用WeakMap表示对象:

function isPrimitive(value) {
    return Object(value) !== value;
}

var primStore = new Map;
var objStore = new WeakMap;

function bindArg(fn, arg) {
    var store = isPrimitive(arg) ? primStore : objStore;
    if (!store.has(arg)) store.set(arg, new WeakMap);
    if (!store.get(arg).has(fn)) store.get(arg).set(fn, fn.bind(null, arg));
    return store.get(arg).get(fn);
}

请注意,我们会返回store.get(arg).get(fn)而不是store.get(fn).get(arg)。这种翻转对bindArg函数的实际语义没有任何影响,但是当我们想要区分原始数据类型和对象时,它是必要的。

修改或者,您可以创建一个新的WeakMap2数据结构,用于存储Map中的原始值和WeakMap中的对象,如下所示:< / p>

var WeakMap2 = defclass({
    constructor: function () {
        this.map1 = new Map;
        this.map2 = new WeakMap;

        if (arguments.length > 0) {
            for (var object in argument[0]) {
                if (isPrimitive(object))
                    throw new TypeError("Iterator value " +
                        object + " is not an entry object");
                else this.set(object[0], object[1]);
            }
        }
    },
    get: function (key) {
        return (isPrimitive(key) ? this.map1 : this.map2).get(key);
    },
    set: function (key, value) {
        return (isPrimitive(key) ? this.map1 : this.map2).set(key, value);
    },
    has: function (key) {
        return (isPrimitive(key) ? this.map1 : this.map2).has(key);
    },
    delete: function (key) {
        return (isPrimitive(key) ? this.map1 : this.map2).delete(key);
    }
});

function defclass(prototype) {
    var constructor = prototype.constructor;
    constructor.prototype = prototype;
    return constructor;
}

function isPrimitive(value) {
    return Object(value) !== value;
}

但是,我不推荐它,因为它增加了一层抽象,使你的代码更慢。