I came across this interesting and weird issue when playing around with a simple object.The type of both the numbers declared as properties of the object obj
are numbers as output in the Chrome dev console. The this
should have referred to the current object.However, when trying to calculate the sum , the result is NaN
instead of the sum. Interestingly, using the object to directly reference the numbers works when calculating the sum.
var obj={
number1: 2,
number2: 3,
sum : this.number1 + this.number2
};
obj.sum;
NaN
BUT the following code works
var obj={
number1: 2,
number2: 3,
sum : obj.number1 + obj.number2
};
obj.sum;
5