I'm trying to build an object in javascript using self-invoking functions to set the object properties – something similar to this:
function Test() {
this.number = 10;
this.square = (function(test) {return test.number * test.number}(this));
}
But when I do this:
var test = new Test();
console.log(test.number + " * " + test.number + " = " + test.square);
test.number = 20;
console.log(test.number + " * " + test.number + " = " + test.square);
The console output is:
10 * 10 = 100
20 * 20 = 100
Why is this? Is there any way to call the self-invoking function more than once? I am trying to use this in order to avoid scoping problems