Inside any declared function (anywhere) and called as follows this will be a window object
function anyFunc(){ alert(this);
If you want to create private functions and access an instance of 'myObject', you can do one of the following methods
One
module = (function () { var privateFunc = function() { alert(this); } var myObject = { publicMethod: function() { privateFunc.apply(this);
Two
module = (function () { var _this;
This is the solution to your problem. I would recommend using prototype based objects.
EDIT:
You can use the first method.
In fact, here myObject is in the same scope as privateFunc , and you can use it directly inside the function
var privateFunc = function() { alert(myObject); }
Below is a real use case of a proxy server for this . You can also use call .
Module = function () { var _this; // proxy variable for instance var privateFunc = function() { alert(this + "," + _this); } this.publicMethod = function() { privateFunc(); // alerts [object Window],[object Object] privateFunc.call(this); // alerts [object Object],[object Object] } _this = this; return this; }; var module = new Module(); module.publicMethod();
Diode source share