我正在创建一个基于GAS Spreadsheets Service的应用程序,它可以读取/写入&更新一行数据。我有一个代表一行数据的键值对象,就像片段中提供的示例数据一样。嵌套函数作为数据原型的对象字面值对象
使用案例:
var exampleData = [{weekendVolume=5186270,midweekVolume=16405609}];
// tuple length 2 of two known values
function _DataRecordObject(exampleData) {
this._endOfWeek = new Date().endOfWeek();// Date.prototype method
}
var _DataRecordMethods = {
weekEnding: function() {
return this._endOfWeek.formatDateString()
},
weekMonth: function() {
return this._endOfWeek.getMonthLabelShort()
},
/* Processed volume */
weekendVolume: function() {
return 'weekendVolume'
},
midweekVolume: function() {
return 'midweekVolume'
},
totalVolumeProcessed: function() {
return _SumTotal(
this.weekendVolume(),
this.midweekVolume()
)
}
}
_DataRecordObject.prototype = _DataRecordMethods;
的new DataRecordObject
是一个表对象,它提供其他有用的性质的原型。 _SumTotal
是一个辅助函数。
我的问题:
当我打电话与片范围作为参数新DataRecordObject,如何更新与新的特性,例如totalVolumeProcessed
的exampleData对象?
例如:
var foo = new _DataRecordObject(exampleData);
Console.log(foo);
//[{weekEnding='Aug-17',weekMonth=4,weekendVolume=5186270,midweekVolume=16405609,totalVolumeProcessed=21591879}]
我想使用构造原型继承,但使用类似对象的文字一个样板式的模板的灵活性。我的直觉表明,我需要在构建新的dataRecordObject时传递数据对象键。
我是JavaScript的新手,尚未掌握继承,原型和各自的设计模式。工厂和模块,或者观察员似乎是合适的模式,但我对JS的有限经验是解决我的问题的限制因素。
'totalVolumeProcessed'不需要更新,这是一种方法。 – Bergi
是的,您可能应该将'exampledata'参数值存储在对象的属性中,就像您使用'this._endOfWeek = ...'所做的一样。 – Bergi
嘿@Bergi感谢您的反馈。因为我认为我的语言具有误导性,所以我重新提出了我的问题。基本上我想使用'_DataRecordMethods'原型提供的方法更新'exampleData'对象。 –