2016-11-24 9 views
0

でない場合、私はいくつかのコードを持っているJavaScriptコードは再使用できますか?は、どのように私はそれが機能

function 1(dat){ 
    call the same code from above 
    v.load(dat) 
} 

function 2(hat){ 
call the same code from above 
change dat to hat 
v.load(hat) 
} 

1 - コードを再利用するにはどうすればよいですか?

2 - どのように私はdatのを作ることができます=第二の機能で帽子

+2

私が知る限り、あなたが望むことをする方法はありません。なぜあなたはそれを機能させることができないのですか? – nottu

+0

関数に入れてもいいですか?次に、あなたの下の関数でそれを呼び出す? –

答えて

0

が機能してください。

function vload(hat_or_dat) { 
    var a = do something with hat or dat 
    b.vr.map.stack = a.stack; 

    var s = a.stack; 
    if (s instance of array) { 
    }else{ 
    } 
    return whatyoudid; 
} 

function one(dat){ 
    // call the same code from above 
    var output = vload(dat); 
    return output; 
} 

function two(hat){ 
    // call the same code from above 
    change dat to hat 
    var output = vload(hat); 
    return output; 
} 

var one_vload_result = one(mydat); 
var two_vload_result = two(myhat); 
関連する問題