What I did
I call a function in objects' value.
And this function would be called in every object as much as users add an absent
data.
What I want to do
Hence I recognize that I could have thousands of hundred of new object, I would rather minimize/eliminate duplication than write the function every time.
Explain what the code is for
The function's name I would like to auto-set is datesBetween()
.
datesBetween()
can choose the mode, and it has parameters.
The mode refers the value from the key reason
which is in same object. arr[0].absent[0].datesBetween
's value should be datesBetween('leave')('2020-1-1','2020-1-4')
.
The parameters does same. First parameter should refer from the object key start
which is in same object, second parameter should refer from the object key end
.
Some idea
- I guess this could use
class
but I am afraid to mix up with functions. if class would help, please do tell. - I write
this
thou, I am uncertain aboutthis
. Is usingthis
could be a solution in this code?var arr = [ { name : "A", absent :[ { reason : "leave", start : '2020-1-1', end : '2020-1-4', datesBetween : datesBetween('leave')(this.start, this.end) }, { reason : "sleeOver", start : '2020-1-25', end : '2020-1-26', datesBetween : datesBetween('sleeOver')(this.start, this.end) } ] }, { name : "B", absent :[ { reason : "weekendExcursion", start : '2020-1-18', end : '2020-1-19', datesBetween : datesBetween('weekendExcursion')(this.start, this.end) } ] } ] function autoAbsentSetter(){ //do I need class to use inheritance/property things? } function addAbsent(mode){ var funcs = { 'leave' : function absent_leave(name, absentName, absentStart, absentEnd){ //all kinds of leave are included. Detail further. //some codes }, 'sleepOver' : function absent_sleepOver(name, absentName, absentStart, absentEnd){ //some codes }, 'weekdayExcursion' : function absent_weekdayExcursion(name, absentName, absentStart, absentEnd){ //some codes }, 'weekendExcursion' : function absent_weekendExcursion(name, absentName, absentStart, absentEnd){ //some codes } } return funcs[mode]; }