Please consider two thing before any comment/answer/vote
Object is within array
I know I can achieve it with
constructor
orfunction
by bindingthis
but I have a condition where I have to rewrite almost 100 lines of existing code just to achieve above functionality. I search almost everything and now asking from Java script Enthusiasts about all possible options with same domain Look at the below object.
Question:I just want total
value to be equal to 7
which in current example is NAN
var demoObj = [{
a: 2,
b: 5,
total: this.a * this.b
}]
how I can achieve it? Please answer with any possible solution not matter how ugly code are I will manage.
Edit Someone try to mark it as duplicate to a given link in comments which is not true because there given answers not suite my needs. even in accepted answer there, if you change the value of a
or b
after initialization then total
will change which not suite my case !