`this` inside a function that isn't a prototype of a class

ohscr

New Member
I'm trying to make a class without prototypes. Here is an example:\[code\]test = (function() { this.value = http://stackoverflow.com/questions/10965485/1; this.print = function() { console.log(this.value); }; return this;})();\[/code\]This works perfectly as intended. What I don't understand is \[code\]this.value\[/code\] inside the \[code\]this.print\[/code\] function. How does \[code\]this.print\[/code\] correctly know that any mention of \[code\]this\[/code\] refers to \[code\]test\[/code\] and not \[code\]window\[/code\]? Would any function defined via \[code\]this.___ = function(){}\[/code\] automatically have \[code\]this\[/code\] added as a context?
 
Top