對於一個項目我的工作,我建立了一些數據對象具有以下佈局設計(這是我與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似乎並不存在那裏。
如果我正確地閱讀了這個問題,它歸結爲如何從該文字中訪問對象文字的屬性。沒有(好)的方法。見例如http://stackoverflow.com/questions/3173610/can-a-javascript-object-property-refer-to-another-property-of-the-same-object。 – georg
你爲什麼把原型裏面的屬性?你想把它們放在構造函數中,使它們對實例來說是唯一的嗎?或者這是一個單身? – elclanrs
rS ..函數在哪裏定義?如果它們是在'AFile.prototype.rS .. = function()...'上定義的,那麼你用你發佈的代碼覆蓋它。如果它被聲明爲this.rS ...那麼你爲AFile的每個實例創建rS ..屬性,即使rS ..函數完全相同並且執行完全相同的操作。 – HMR