如何从类似的方法中删除冗余?

时间:2014-07-24 20:52:59

标签: javascript methods

有关如何删除此对象中方法冗余的任何创意建议?:

var ObjectCrud = {
    createByKey: function(object, word, new_key, new_val) {
        return iterativeFunction(object, function (key, obj) {
            if (key === word) {
                obj[new_key] = new_val;
            }
        });
    },
    readByKey: function(object, word) {
        var object_array = [];
        iterativeFunction(object, function (key, obj) {
            if (key === word) {
                 object_array.push(obj);
            }
        });
        if (object_array.length > 0) { return object_array; }
    },
    updateByKey: function(object, word, update) {
        return iterativeFunction(object, function (key, obj) {
            if (key === word) {
                obj[key] = update;
            }
        });
    },
    deleteByKey: function(object, word) {
        return iterativeFunction(object, function (key, obj) {
            if (key === word) {
                delete obj[key];
            }
        });
    },
    createByValue: function(object, word, new_key, new_val) {
        return iterativeFunction(object, function (key, obj) {
            if (obj[key] === word) {
                obj[new_key] = new_val;
            }
        });
    },
    readByValue: function(object, word) {
        var object_array = [];
        iterativeFunction(object, function (key, obj) {
            if (obj[key] === word) {
                 object_array.push(obj);
            }
        });
        if (object_array.length > 0) { return object_array; }
    },
    updateByValue: function(object, word, update) {
        return iterativeFunction(object, function (key, obj) {
            if (obj[key] === word) {
                obj[key] = update;
            }
        });
    },
    deleteByValue: function(object, word) {
        return iterativeFunction(object, function (key, obj) {
            if (obj[key] === word) {
                delete obj[key];
            }
        });
    }
}

我的代码对我有用,我觉得这是一种更有表现力的方式来编写上面列出的方法。对于想知道这个对象会做什么的人来说,它的目标是在js对象上进行与持久性存储概念相关的操作。

1 个答案:

答案 0 :(得分:0)

好吧,您似乎希望对iterativeFunction进行抽象,总是使用if-test进行回调。

所以让我们为它编写一个函数(或者实际上是两个函数,因为你曾经比较过键和一次值)。

function findKey(object, word, callback) {
    return iterativeFunction(object, function (key, obj) {
        if (key === word)
            callback(key, obj);
    });
}
function findValue(object, word, callback) {
    return iterativeFunction(object, function (key, obj) {
        if (obj[key] === word)
            callback(key, obj);
    });
}

var ObjectCrud = {
    createByKey: function(object, word, new_key, new_val) {
        return findKey(object, word, function(key, obj) {
            obj[new_key] = new_val;
        });
    },
    readByKey: function(object, word) {
        var object_array = [];
        findKey(object, word, function(key, obj) {
            object_array.push(obj);
        });
        if (object_array.length > 0) { return object_array; }
    },
    updateByKey: function(object, word, update) {
        return findKey(object, word, function(key, obj) {
            obj[key] = update;
        });
    },
    deleteByKey: function(object, word) {
        return findKey(object, word, function(key, obj) {
            delete obj[key];
        });
    },
    createByValue: function(object, word, new_key, new_val) {
        return findValue(object, word, function(key, obj) {
            obj[new_key] = new_val;
        });
    },
    readByValue: function(object, word) {
        var object_array = [];
        findValue(object, word, function(key, obj) {
            object_array.push(obj);
        });
        if (object_array.length > 0) { return object_array; }
    },
    updateByValue: function(object, word, update) {
        return findValue(object, word, function(key, obj) {
            obj[key] = update;
        });
    },
    deleteByValue: function(object, word) {
        return findValue(object, word, function(key, obj) {
            delete obj[key];
        });
    }
}

我们也看到回调很复杂。对于delete,它们实际上完全相同,其他的具有重复的主体但范围不同。此外,objectword参数以及findKeyiterativeFunction调用仍然在各处复制(我实际上在上面的示例中使用了c& p)。

现在,我们可以通过应用一些函数式编程或部分应用程序来确定目标,并使所有内容更简洁:

function makefinder(compare) {
    return function makeCrud(callback) {
        return function(object, word, arg1, arg2) {
            return iterativeFunction(object, function (key, obj) {
                if (compare(key, obj, word))
                    callback(key, obj, arg1, arg2);
            });
        };
    };
}
var findKey = makeFinder(function(key, _, word) { return key===word; });
var findValue = makeFinder(function (key, obj, word) { return obj[key] === word; });

function create    (_,   obj, new_key, new_val) { obj[new_key] = new_val; }
function read      (key, obj, object_array)     { object_array.push(obj); }
function update    (key, obj, update)           { obj[key] = update; }
function deleteProp(key, obj)                   { delete obj[key]; }

function makeReader(finder) {
    var reader = finder(read);
    return function(object, word) {
        var array = [];
        reader(object, word, array);
        if (array.length) return array;
    };
}

var ObjectCrud = {
    createByKey: findKey(create),
    readByKey:   makeReader(findKey),
    updateByKey: findKey(update),
    deleteByKey: findKey(deleteProp),
    createByValue: findValue(create),
    readByValue:   makeReader(findValue),
    updateByValue: findValue(update),
    deleteByValue: findValue(deleteProp)
};

当然,你也可以通过在一个矩阵中循环ObjectCrud并在另一个维度[Key, Value]中循环来创建[create, read, update, delete] ...