I have an object that looks like this:
var foo = {
parent: {
childOne: {
prop: 1,
doSomething: function(){
return this.prop;
}
},
childTwo: {
prop: 2,
doSomething: function(){
return this.prop;
}
}
},
other: {
action: function(){
return foo.parent.childOne.doSomething() +
foo.parent.childTwo.doSomething();
}
}
}
window.alert(foo.other.action());
Here the doSomething() function is definitely duplicate code and I would like to avoid it, this is something similar to what inheritance solves.
I was thinking if there is any way of doing something along the lines of:
parent: {
doSomething: function(){
return this.prop;
}
}
But not sure how to actually implement it, is this possible?
return foo.parent.childOne.prop + foo.parent.childTwo.prop;and you are rid of the doSomething repetition...