2017-04-06 99 views
3

我的目標是有效地將一組動態選擇的變換應用於矩陣的每個元素。我將選定的函數存儲在一個數組中,然後通過矩陣將它們中的每一個應用於一次。在Javascript中,如何應用動態創建的函數名稱

我的問題是,我該如何動態創建一個函數的名稱,我將它添加到函數數組中?

這個fiddle包含我的嘗試。我的問題包含在評論欄中。

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); 
} 

回答

2

我喜歡我的聲明功能的哈希在這種情況下,那麼你就可以根據傳遞的價值,這是關鍵的哈希打電話給他們。

更新:我已經添加了一種方法來動態獲取低/高功能,並調用它。

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));

+0

謝謝。雖然我很欣賞這個解決方案的有效性,但我仍然想知道是否有一種方法可以使用文本串聯來實際創建函數名稱。 –

+0

我已經在那裏添加了你想要的東西。基本上,你將這個名字連接到String.prototype中,如下所示:var lowerUpperFunction = String.prototype ['to'+ whichCase +'Case'];並像這樣調用它: var str = lowerUpperFunction.call(「xyz」); – nixkuroi

+0

謝謝你,nixkuroi。這看起來像我想要的。現在已經晚了,所以明天我會玩。我感謝您的幫助! –