[英]In Javascript, how to apply a dynamically created function name
我的目標是有效地將動態選擇的變換集應用於矩陣的每個元素。 我將選定的函數存儲在一個數組中,然后通過矩陣一次性地應用它們。
我的問題是,如何動態創建我添加到函數數組的函數的名稱?
這個小提琴包含了我的嘗試。 我的問題包含在評論欄中。
function dynam () {
var prepend = 'before - ';
var append = ' - after';
var whichCase = 'Upper';
var functionsToApply = [];
var matrix = [
['aBc', 'DeF'],
['ghi', 'JKL'],
];
var out = 'Initial: ' + matrix.join(' | ');
document.getElementById('output').innerHTML = out + '\n';
console.log(out);
// Set up transforms
if (whichCase == 'Lower') {
functionsToApply.push(function(v) {return v.toLowerCase();});
} else if (whichCase == 'Upper'){
functionsToApply.push(function(v) {return v.toUpperCase();});
}
// How can the function be defined dynamically?
// Perhaps something like:
// if(['Lower','Upper'].indexOf(whichCase) != -1) {
// functionsToApply.push(function(v) {'return v.to' + which + 'Case();'});
// }
if (prepend && prepend.length > 0 && prepend != 'none') {
functionsToApply.push(function(v) {return prepend + v;});
}
if (append && append.length > 0 && append != 'none') {
functionsToApply.push(function(v) {return v + append;});
}
// Apply all of the transforms to each of the elements of the matrix
matrix = matrix.map(function(row){
return row.map(function(val) {
for (var fn = 0; fn < functionsToApply.length; fn++) {
val = functionsToApply[fn](val);
}
return val;
})
});
out = 'Final: ' + matrix.join(' | ');
document.getElementById('output').innerHTML += out + '\n';
console.log(out);
}
在這種情況下,我喜歡在哈希中聲明我的函數,然后你可以根據傳入的值來調用它們,這是哈希的關鍵。
更新:我添加了一種動態獲取下/上功能的方法,並調用它。
function dynam(whichCase, prepend, append, matrix) { var functionHash = { "Lower" : function(v) {return v.toLowerCase();}, "Upper" : function(v) {return v.toUpperCase();}, "Prepend" : function(v) {return prepend + v;}, "Append": function(v) {return v + append;} } // to actually get the case function based on the word passed in, // you can do it this way. var lowerUpperFunction = String.prototype['to' + whichCase + 'Case']; var str = lowerUpperFunction.call("xyz"); console.log(str); var functionsToApply = []; var out = 'Initial: ' + matrix.join(' | '); console.log(out); // see how we just take the whichCase and make that a call to the hash? functionsToApply.push(functionHash[whichCase]); if (prepend && prepend.length > 0 && prepend != 'none') { functionsToApply.push(functionHash["Prepend"]); } if (append && append.length > 0 && append != 'none') { functionsToApply.push(functionHash["Append"]); } // Apply all of the transforms to each of the elements of the matrix matrix = matrix.map(function(row){ return row.map(function(val) { for (var fn = 0; fn < functionsToApply.length; fn++) { console.log("applying function to val" + val ); val = functionsToApply[fn](val); } return val; }) }); out = 'Final: ' + matrix.join(' | '); return out; } var p = 'before - '; var a = ' - after'; var w = 'Upper'; var m = [ ['aBc', 'DeF'], ['ghi', 'JKL'], ]; console.log( dynam(w, p, a, m) );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.