2013-06-19 88 views
0

對於一個項目我的工作,我建立了一些數據對象具有以下佈局設計(這是我與ArrayBuffers讀二進制文件:訪問返回對象

AFile.prototype = { 
    p: new BufferPack(), 
    filedata: null, 
    position: 0, 
    label_records: null, 
    closestmultipleof: function(n,v) { 
     return Math.ceil((v/n) * n); 
    }, 
    r: function(size) { 
     result = new Uint8Array(this.filedata,this.position,size); 
     this.position += size; 
     return result; 
    } 
    readValueLabel: function() { 
     return { 
      value: this.rS(8), 
      len: this.rS8(), 
      label: this.rS(this.closestmultipleof(8, this.len + 1)) 
     }; 
    }, 
    readLabelRecords: function() { 
     return { 
      rec_type: this.rS32(), 
      label_count: this.rS32(), 
      value_labels: _.map(_.range(this.label_count), function(num) { 
       console.debug(num); 
      },this) 
     }; 
    }, 
    loadFile: function(blob) { 
     this.filedata = blob; 
     this.label_records = this.readLabelRecords(); 
    } 
}; 

但是,我似乎有在返回範圍訪問值的問題。在某些回報範圍,我需要從同一個範圍內訪問,以操縱數據一點點(見value_labels的定義)的變量。

只有

,它似乎不能夠有訪問變量LABEL_COUNT(可能是因爲它是在相同的返回範圍)。我將如何能夠做到這一點?

,我可以得到它的工作的唯一辦法是,如果我這樣做:

ret = {} 
ret['a'] = 5; 
ret['b'] = ret['a'] * 2 
return ret; 

但是,這似乎很醜陋。有任何想法嗎?

是的,它是一個單!我只打算使用一次。

讓我明白:問題是下面的代碼中:

return { 
    a: functionreturn(), 
    b: this.a * s 
}; 

This.a似乎並不存在那裏。

+0

如果我正確地閱讀了這個問題,它歸結爲如何從該文字中訪問對象文字的屬性。沒有(好)的方法。見例如http://stackoverflow.com/questions/3173610/can-a-javascript-object-property-refer-to-another-property-of-the-same-object。 – georg

+0

你爲什麼把原型裏面的屬性?你想把它們放在構造函數中,使它們對實例來說是唯一的嗎?或者這是一個單身? – elclanrs

+0

rS ..函數在哪裏定義?如果它們是在'AFile.prototype.rS .. = function()...'上定義的,那麼你用你發佈的代碼覆蓋它。如果它被聲明爲this.rS ...那麼你爲AFile的每個實例創建rS ..屬性,即使rS ..函數完全相同並且執行完全相同的操作。 – HMR

回答

1

[更新] 您可以創建一個封閉到LABEL_COUNT。

function AFile(){}; 
AFile.prototype ={ 
    readLabelRecords: function() { 
     label_count=this.rS32(); 
     return { 
      label_count:label_count, 
      log:console.log(label_count)//'return from rs32' 
     }; 
    }, 
}; 
AFile.prototype.rS32=function(){ 
    return "return from rs32"; 
} 
var o = new AFile(); 
o.readLabelRecords(); 

這個答案的基礎上提供的代碼,最簡單的代碼重新產生:

function complicatedCalculations(){ 
    return 22; 
} 
function returnObject(){ 
    var cacheComplicated=complicatedCalculations();//closure variable will 
     // be available within the entire body of returnObject function 
     // but never outside of it. 
    return{ 
    calculated:cacheComplicated, 
    twiceCalculated:cacheComplicated*2//you could not access calculated 
          // here so using a cache closure variable 
    } 
} 

或者讓你返回object函數返回一個構造函數創建的對象的新實例:

function returnObject(){ 
    return new (function(){ 
    this.calculated=complicatedCalculations(); 
    this.twiceCalculated=this.calculated*2; 
    })(); 
} 
+0

完美!這似乎工作。不得不聲明所有變量兩次,但它可能會更糟! –

+0

你可以'返回新(函數(){this.label_count = 22; this.double_label_count = this.label_count * 2;})();'不需要聲明變量兩次。 – HMR

+0

@MatsWillemsen更新了我的答案,以返回使用構造函數創建的對象實例。不需要在同一個對象中使用兩次聲明變量。 – HMR

0

readValueLabel這使得結構無效之前忘了一個逗號。

更新

太糟糕了,對方的回答被刪除,它有即使它沒有「編譯」的有效點。 參考this是JS內部範圍內的問題,但它可以通過周圍做類似的東西來工作:

readLabelRecords: function() { 
     var that = this; 
     return { 
      rec_type: that.rS32(), 
      label_count: that.rS32(), 
      value_labels: _.map(_.range(that.label_count), function(num) { 
       console.debug(num); 
      },that) 
     }; 
    }