0

オブジェクト メソッドが期待どおりに実行されないという問題があります。を実行するndData.recipe()と、期待どおりにオブジェクトが更新ndData.recipeDataされ、以前の値に新しい値が追加されます。

しかし、 を呼び出して再度ndData.update()実行ndData.recipe()すると、新しい値が古い値に追加されるのではなく、古い値が新しい値に置き換えられます。

私のエラーは、変数の不適切なスコープに関係している可能性があると思いますが、よくわかりません。

var ndData = {

initData: null,
newData: null,
recipeData: null,
ingredients : 0,

load : function(NDB_No){

    var options = [];
    $.getJSON(('scripts/jsonencode.php?q=' + NDB_No), function(data) {
        $.each(data.measurements, function(key, val) {
            options.push('<option id="wgt' + val.Seq + '" value="' + val.Gm_Wgt + '">'+ val.Amount + ' ' + val.Msre_Desc + '</option>');
            options.join('');
            $('#servSelect').html(options);
         }); 

        ndData.initData = data;
        ndData.newData = data;
        ndData.recipeData = data;

        ndData.print(ndData.initData, '#ndDataTbl');

      });

},

print : function(pdata, div){
    var items=[];
    var options=[];
    var wtRatio = 1;

    $.each(pdata.properties, function(key, val) {

        if ($.isNumeric(val)){
            val = Math.round((val * wtRatio)*10)/10;
            }

        items.push('<tr>');
        items.push('<td id="">'+ key +" : " + val + '</td>');
        items.push('</tr>');
    });

  $(div).html((items.join('')));
},


//Seems that when this update() function is called, ndData.recipeData is set to ndData.newData


update : function(){ 

    var GmWt = ($('#servSelect').val())*($('#servQty').val());
    var wtRatio = GmWt/ndData.initData.properties.GmWt_1;

    $.each(ndData.newData.properties, function(key, val) {

        if ($.isNumeric(val)){
            parseFloat(val);
            parseFloat(wtRatio);

            val = Math.round((val * wtRatio)*10)/10;
            }

        ndData.newData.properties[key] = val;

    });
    console.log('update val: ' + ndData.newData.properties.Refuse_Pct);
    ndData.print(ndData.newData, '#ndDataTbl');
},

recipe : function(){

    if (ndData.ingredients > 0){
        console.log("inregedients > 0"); 

         $.each(ndData.newData.properties, function(key, val){

            if($.isNumeric(val)){
                val = parseFloat(val);
                ndData.recipeData.properties[key] = parseFloat(ndData.recipeData.properties[key]);
                ndData.recipeData.properties[key] += val;

                console.log('val: ' + ndData.recipeData.properties[key]); // after ndData.update() is called,
                                                                          // this is logging the value of ndData.newData.properties[key]
            }
        });

    }else{
       console.log("inregedients == 0");
       ndData.recipeData = ndData.newData; 
    }


// after calling ndData.update(), this is printing properties of ndData.newData,
// when I expect properties of ndData.recipeData

 ndData.print(ndData.recipeData, '#recipeDataTbl');

 ndData.ingredients++ ;

}
};
4

1 に答える 1

0

クラスをオブジェクトリテラルとして設定します。これは悪いことではありませんが、オブジェクトリテラルが機能する方法は、そのインスタンスは1つしか存在できず、そこに入るデータはすべて置き換えられるということです。

クラスを作成する他の方法を調べたり、stackoverflowでこのスレッドを調べて、オブジェクトリテラルについてもう少し理解したりすることもできます。

于 2013-03-03T22:18:49.170 に答える