2017-08-07 125 views
0

我是node.js的初學者,在評估evalMath()函數後出現問題。尤其是,將Math2的值賦值給yScale將值賦給node.js中的變量

Math2是一個網頁的輸入框,它引入了一個簡單的等式,如'Ir = Vr/R'。該字符串值從網頁傳遞到node.js上實現的Web服務器。在那裏,函數evalMath()被執行,它用一個相應的整數值代替Vr(用一個字符串)和R代替。在執行這個函數之後得到像這樣的'ANI0 [i] -ANI5 [i]/10',它存儲在變量Math2中。該功能完美工作。但是,當將Math2分配給yScale時,只保存'Ir = Vr/R',而不是執行evalMath() - >'ANI0 [i] -ANI5 [i]/10'後獲得的字符串。我不知道爲什麼。

if (yScale == 'Math2') { 
    console.log('Math2 selected'); 
    evalMath(textAD0,textAD1,textAD2,textAD3,textAD4,Math2,Rb,Rc,R,yScale); 
    yScale = Math2; 
    console.log ("yScale: "+ yScale); 
} else { 
    console.log("Nothing to change :) - No eqn"); 
} 

function evalMath(labelAD0, labelAD1, labelAD2, labelAD3, 
       labelAD4, entry, entry1, entry2, entry3, entry4) { 
    iAD0="",iAD1="",iAD2="",iAD3="",iAD4="",iR="",iRb="", iRc="", iEq=""; 

    var iAD0 = entry.search(labelAD0); 
    var iAD1 = entry.search(labelAD1); 
    var iAD2 = entry.search(labelAD2); 
    var iAD3 = entry.search(labelAD3); 
    var iAD4 = entry.search(labelAD4); 
    var iR = entry.search('R'); 
    var iRb = entry.search('Rb'); 
    var iRc = entry.search('Rc'); 
    var iEq = entry.search('=');  

    if (iAD0 > "0"){ entry = entry.replace(labelAD0,'(ANI0[i]-ANI5[i])'); } 
    else{console.log("Nothing to change AD0")} 
    if (iAD1 > "0"){ entry = entry.replace(labelAD1,'(ANI1[i]-ANI5[i])'); } 
    else{console.log("Nothing to change AD1")} 
    if (iAD2 > "0"){ entry = entry.replace(labelAD2,'(ANI2[i]-ANI5[i])'); } 
    else{console.log("Nothing to change AD2")} 
    if (iAD3 > "0"){ entry = entry.replace(labelAD3,'(ANI3[i]-ANI5[i])'); } 
    else{console.log("Nothing to change AD3")} 
    if (iAD4 > "0"){ entry = entry.replace(labelAD4,'(ANI4[i]-ANI5[i])'); } 
    else{console.log("Nothing to change AD4")} 
    if (iRb > "0"){ entry = entry.replace('Rb',entry1); } 
    else{console.log("Nothing to change Rb")} 
    if (iRc > "0"){ entry = entry.replace('Rc',entry2); } 
    else{console.log("Nothing to change Rc")} 
    if (iR > "0"){ entry = entry.replace('R',entry3); } 
    else{console.log("Nothing to change R")} 
    if (iEq > "0"){ entry = entry.slice(iEq+1,entry.length); } 
    else{console.log("Nothing to change iEq")} 

    console.log("entry: " + entry); 
    return entry; 
} 

回答

0

當您將參數傳遞給函數時,只傳遞該值而不是變量的引用(對於字符串,整型,浮點型等)。如果你希望能夠修改參數的值,傳遞給一個對象:

var a = "hello"; 
var b = "moto"; 

var obj = {a: "hello", b: "moto"}; 

function c(p1, p2){ p1 = "yala"; p2="yele";} 

c(a, b); // a == hello, b == moto, values not changed 

function c2(p1) { p1.a = "yala"; p1.b = "yele";} 

c2(obj); // obj.a == "yala", obj.b == "yele" 

所以你的情況做到以下幾點:

var entries = { 
Math2: Math2, 
Rb: Rb, 
Rc: Rc, 
R: R 
yScale: yScale 
}; 


evalMath(textAD0,textAD1,textAD2,textAD3,textAD4,entries); 
entries.yScale = entries.Math2; 
console.log ("yScale: "+ entries.yScale); 

然後用

取代evalMath的原型
function evalMath(labelAD0, labelAD1, labelAD2, labelAD3, 
      labelAD4, entries) { 

然後在evalMath中,用entry.Math2替換所有entry1的出現,所有entry2出現entry.Rb,所有entry3出現entry.Rc,所有entry.R出現entry.R和所有發生的入口5入口.yScale

如果您影響某些值的條目,則會發生警告。將其修改爲功能