I have the following error when running the page below:
\"this.testpublic is not a function\"
test = function() {
var testprivate = function() {
The problem is that this
actually never was referring to the test
object to begin with. It was always referring to the nearest enclosing object -- which in this case is window
.
test = function() {
var testprivate = function(say) {
console.log('test', say);
}
this.testpublic = function(say) {
testprivate('test', say);
}
testprivate();
}
x = new test();
This works because, as I understand it, this
is determined at call time -- and it is locked into the nearest "enclosing" object*, unless call()
or apply()
is used.
*
There is probably a much better word for this, but I don't know it off the top of my head. If someone knows, please enlighten us all :-)