2016-05-21 5 views
0

だから私は説明できないこの奇妙な振る舞いをしています。以下を見てください:関数内の変数を宣言する

$("#completeData").on("click", function() { 
    var toUpdate = {}; 
    var toUpdateCount = 0; 
    var ratios = {}; 
    This.calculateGradePerSize(); 
    //1) Select all sizes that are equal to NA or are Equal to 0 (means its a new one) 
    $.each(This.logements, function(key, l) { 
    if (l.sizeMyId === "NA" || l.sizeMyId === 0) { 
     toUpdate[l.rueNum] = l; 
     toUpdateCount++; 
    } else { //else init the ratios because it means they are actually present 
     /** 
     //My problem is this variable, 
     I want it to be equal to an empty object 
     But for reasons I cannot seem to understand, 
     it takes in account the latter modification in the code 
     that happens to this variables 
     */ 
     ratios[l.sizeMyId] = {}; 
    } 
    }); 

    console.log(toUpdate); 
    console.log(ratios); 
    console.log(This.sizeRatio); 

    //2) Calculate Ratios and build the ratios function of the toUpdate 
    $.each(This.sizeRatio, function(sizeMyId, count) { 
    if (sizeMyId !== "NA" && sizeMyId != 0) { 
     console.log("COUNT SIZE: " + count + " COUNT LOGEMENT: " + This.countLogement + " toUpdateCount: " + toUpdateCount + " SizeMyId: " + sizeMyId); 
     console.log("Calculation: " + count/This.countLogement * toUpdateCount); 
     ratios[sizeMyId].count = Math.ceil(count/This.countLogement * toUpdateCount); 
     console.log("Calculation WITH CEIL: " + Math.ceil(count/This.countLogement * toUpdateCount)); 
     ratios[sizeMyId].grade = This.sizeGrade[sizeMyId]; 
     ratios[sizeMyId].sizeMyId = sizeMyId; 
    } 
    }); 

    console.log(ratios); 
}); 

私の問題は倍率変数です。私はvarプレフィックスなしで変数を宣言してみましたので、JSはその存在を知りませんが、まだ空のオブジェクトにしておきたいと思います。実際、この問題は単にそれを根付かせるよりも根強く、私はそれを更新することはできません。 ratios varに行った変更は登録されていませんが、最初から始めたいのですが、この変数が関数の先頭で空であることを確認するにはどうすればよいですか?

+0

あなたが 比[sizeMyId] .sizeMyIdオブジェクトと配列の両方としての比率を扱っているようです。比率= {}; - 彼らは同じではありません。 – Ozoid

+0

しかし、 '[]'表記を使わずにオブジェクトの要素を動的に呼び出す方法はありません。私は本当に 'ratios.DYNAMIC_PROPERTY'を行うことはできません。さもなければ、それは新しいものと考えるでしょう。 – delmalki

答えて

1

この質問が本当に価値があるかどうかわかりません。それを削除することを考える。私のバグは、各機能のカウント変数と比率定義が同じで、登録していないということでした。

関数起動時に空でない変数は、これは、JSエンジンがどのように機能するかを示しています。動作していないものがある場合は、コードに何か問題があります。

$.each(This.sizeRatio, function (sizeMyId, count) { 
    if (sizeMyId !== "NA" && sizeMyId != 0) { 
    console.log("COUNT SIZE: " + count + " COUNT LOGEMENT: " + This.countLogement + " toUpdateCount: " + toUpdateCount + " SizeMyId: " + sizeMyId); 
    console.log("Calculation: " + count/This.countLogement * toUpdateCount); 
    //HERE ratios[sizeMyId].count IS THE SAME than the anonymous function. 
    ratios[sizeMyId].count = Math.ceil(count/This.countLogement * toUpdateCount); 
    console.log("Calculation WITH CEIL: " + Math.ceil(count/This.countLogement * toUpdateCount)); 
    ratios[sizeMyId].grade = This.sizeGrade[sizeMyId]; 
    ratios[sizeMyId].sizeMyId = sizeMyId; 
    } 
}); 
+1

ナアマンはそれを保つ。偉大なプログラマーでさえ時々 "夢中になる"瞬間を覚えているあなたのタイトルを検索する人を見せてください。 – Jhecht

関連する問題