Fairly new to using a prototype design pattern so I had a quick question about the most efficient way of creating a setter/getter for multiple constructor variables.
Say I had a "trip" object :
function trip(){
this.numberOfDays = 0
this.price = 0
this.activites = []
this.bPacks = []
this.pPacks = []
}
Now the setters for the activities, bPacks, and pPacks all do primarily the same thing. They will add or remove to the array that they are associated with.
so instead of writing 6 separate setters for these properties such as :
trip.prototype.addActivity = function(itemToAdd){
var index = this.activites.push(itemToAdd)
}
trip.prototype.addBPack = function(itemToAdd){
var index = this.bPack.push(itemToAdd)
}
etc...
Is it possible to do something like this :
trip.prototype.addPacksOrActivity = function(item,typeOfItem){
this.typeOfItem.push(item);
}
where we target the specific property of the trip object we want to push to?
Any suggestions for more efficient ways of building setters/getters are welcome but I am trying to avoid transpiling from ES6 so ES5 type answers are preffered.
Aucun commentaire:
Enregistrer un commentaire