JavaScript 从两个函数参数重构为多个函数参数
JavaScript Refactor From Two to Many Function Arguments
这两个 JavaScript 函数每个接受两个数组参数和 return 一个数组结果。符合 ES3,我如何重写这些以接受无限数量的数组输入?
function sum(v, w) {
for (var a = jsArray(v), b = jsArray(w), t = 0; t < a.length; t++) a[t] += b[t];
return vbArray(a);
}
function mul(v, w) {
for (var a = jsArray(v), b = jsArray(w), t = 0; t < a.length; t++) a[t] *= b[t];
return vbArray(a);
}
奇数 jsArray()
函数是必需的,因为要处理的数组来自 VBA 并且 jsArray() 将它们转换为 JavaScript 数组:
function jsArray(v) {
return new VBArray(v).toArray()
}
您可以尝试使用类数组对象 arguments 以获取所有传递的参数:
function someFunc() {
for (var i=0; i < arguments.length; i++) {
console.log(arguments[i]);
}
}
someFunc('a', 'b', 'c');
转换后的 sum
函数示例。请注意,如果所有传递的数组具有相同的长度,这将起作用。
function sum() {
var arrays = [];
for (var i = 0; i < arguments.length; i++) {
arrays[i] = jsArray(arguments[i]);
}
for (var j = 1; j < arrays.length; j++) {
for (var t = 0; t < arrays[0].length; t++) {
arrays[0][t] += arrays[j][t];
}
}
return vbArray(arrays[0]);
}
通用解决方案:
function process(concreteFunc) {
var arrays = [];
for (var i = 1; i < arguments.length; i++) {
arrays.push(jsArray(arguments[i]));
}
for (var j = 1; j < arrays.length; j++) {
for (var t = 0; t < arrays[0].length; t++) {
arrays[0][t] = concreteFunc(arrays[0][t], arrays[j][t]);
}
}
return vbArray(arrays[0]);
}
var sum = process.bind(null, function (a, b) {return a + b});
var mul = process.bind(null, function (a, b) {return a * b});
没有.bind
的解决方案:
function process(concreteFunc, args) {
var arrays = [];
for (var i = 0; i < args.length; i++) {
arrays.push(jsArray(args[i]));
}
for (var j = 1; j < arrays.length; j++) {
for (var t = 0; t < arrays[0].length; t++) {
arrays[0][t] = concreteFunc(arrays[0][t], arrays[j][t]);
}
}
return vbArray(arrays[0]);
}
function createFunc(handler) {
return function() {
return process(handler, Array.prototype.slice.call(arguments));
}
}
var sum = createFunc(function (a, b) {return a + b});
var mul = createFunc(function (a, b) {return a * b});
改进版本以支持实施 avg
:
function process(concreteFunc, args) {
var arrays = [];
for (var i = 0; i < args.length; i++) {
arrays.push(jsArray(args[i]));
}
var result = [];
for (var j = 0; j < arrays[0].length; j++) {
var items = [];
for (var t = 0; t < arrays.length; t++) {
items.push(arrays[t][j]);
}
result.push(concreteFunc(items));
}
return vbArray(result);
}
function createFunc(handler) {
return function() {
return process(handler, Array.prototype.slice.call(arguments));
}
}
function reduce(items, handler) {
var result = items[0];
for (var i = 1; i < items.length; i++) {
result = handler(result, items[i]);
}
return result;
}
var sum = createFunc(function(items) {
return reduce(items, function (a, b) {return a + b});
});
var mul = createFunc(function(items) {
return reduce(items, function (a, b) {return a * b});
});
var avg = createFunc(function(items) {
return reduce(items, function (a, b) {return a + b}) / items.length;
});
这两个 JavaScript 函数每个接受两个数组参数和 return 一个数组结果。符合 ES3,我如何重写这些以接受无限数量的数组输入?
function sum(v, w) {
for (var a = jsArray(v), b = jsArray(w), t = 0; t < a.length; t++) a[t] += b[t];
return vbArray(a);
}
function mul(v, w) {
for (var a = jsArray(v), b = jsArray(w), t = 0; t < a.length; t++) a[t] *= b[t];
return vbArray(a);
}
奇数 jsArray()
函数是必需的,因为要处理的数组来自 VBA 并且 jsArray() 将它们转换为 JavaScript 数组:
function jsArray(v) {
return new VBArray(v).toArray()
}
您可以尝试使用类数组对象 arguments 以获取所有传递的参数:
function someFunc() {
for (var i=0; i < arguments.length; i++) {
console.log(arguments[i]);
}
}
someFunc('a', 'b', 'c');
转换后的 sum
函数示例。请注意,如果所有传递的数组具有相同的长度,这将起作用。
function sum() {
var arrays = [];
for (var i = 0; i < arguments.length; i++) {
arrays[i] = jsArray(arguments[i]);
}
for (var j = 1; j < arrays.length; j++) {
for (var t = 0; t < arrays[0].length; t++) {
arrays[0][t] += arrays[j][t];
}
}
return vbArray(arrays[0]);
}
通用解决方案:
function process(concreteFunc) {
var arrays = [];
for (var i = 1; i < arguments.length; i++) {
arrays.push(jsArray(arguments[i]));
}
for (var j = 1; j < arrays.length; j++) {
for (var t = 0; t < arrays[0].length; t++) {
arrays[0][t] = concreteFunc(arrays[0][t], arrays[j][t]);
}
}
return vbArray(arrays[0]);
}
var sum = process.bind(null, function (a, b) {return a + b});
var mul = process.bind(null, function (a, b) {return a * b});
没有.bind
的解决方案:
function process(concreteFunc, args) {
var arrays = [];
for (var i = 0; i < args.length; i++) {
arrays.push(jsArray(args[i]));
}
for (var j = 1; j < arrays.length; j++) {
for (var t = 0; t < arrays[0].length; t++) {
arrays[0][t] = concreteFunc(arrays[0][t], arrays[j][t]);
}
}
return vbArray(arrays[0]);
}
function createFunc(handler) {
return function() {
return process(handler, Array.prototype.slice.call(arguments));
}
}
var sum = createFunc(function (a, b) {return a + b});
var mul = createFunc(function (a, b) {return a * b});
改进版本以支持实施 avg
:
function process(concreteFunc, args) {
var arrays = [];
for (var i = 0; i < args.length; i++) {
arrays.push(jsArray(args[i]));
}
var result = [];
for (var j = 0; j < arrays[0].length; j++) {
var items = [];
for (var t = 0; t < arrays.length; t++) {
items.push(arrays[t][j]);
}
result.push(concreteFunc(items));
}
return vbArray(result);
}
function createFunc(handler) {
return function() {
return process(handler, Array.prototype.slice.call(arguments));
}
}
function reduce(items, handler) {
var result = items[0];
for (var i = 1; i < items.length; i++) {
result = handler(result, items[i]);
}
return result;
}
var sum = createFunc(function(items) {
return reduce(items, function (a, b) {return a + b});
});
var mul = createFunc(function(items) {
return reduce(items, function (a, b) {return a * b});
});
var avg = createFunc(function(items) {
return reduce(items, function (a, b) {return a + b}) / items.length;
});