javascript - JS: Modify Value/Pair in JS Object -


i'm trying work out best way modify object without writing out similar object 3 times. have these 3 objects:

var object1 = {     start: start,     end: end,     type: 1 }  var object2 = {     start: start,     end: end,     type: 2 }  var object3 = {     start: start,     end: end,     type: 3 } 

the thing changes type. there better way write i'm not repeating myself?

you can set common properties prototype object. example:

function objectmaker (typeval) {   this.type = typeval; }  objectmaker.prototype.start = "start"; objectmaker.prototype.end = "end";  var object1 = new objectmaker("1"); var object2 = new objectmaker("2"); 

gives

> object1.start "start" > object1.end "end" > object1.type "1" 

you pass in object maker function if number of variables more.

since prototype shared across objects, have lighter memory footprint having same on each object.


Comments