You're calling getSomething() on the constructor, not the object. It doesn't look like you ever actually instantiate InnerObject, actually. If InnerObject is supposed to point to whatever this function returns (instead of to the actual function), then you need to make InnerObject into an immediately-invoked function expression (IIFE), like so:
function MainObject() {
var id;
...
var InnerObject = (function() {
this.getSomething = function() { ... }
}());
this.testFunc = function() {
id = setTimeout(InnerObject.getSomething(), 1000);
}
}
Now InnerObject points to the result of calling the function you wrote, which will have a this.getSomething
member that you can see in the constructor (but nowhere else, since InnerObject is still just a variable inside the constructor).
The catch here is that because I used an IIFE, InnerObject only gets instantiated once. If you need to instantiate several of them from the same constructor, then you need to keep a handle around. You can still do this while being private, however. One possibility might look like:
function MainObject() {
var id;
...
function InnerObject() {
this.getSomething = function() { ... }
};
var inner1 = new InnerObject(),
inner2 = new InnerObject(),
inner3 = new InnerObject();
this.testFunc = function() {
id = setTimeout(inner1.getSomething(), 1000);
}
}
inner1
, inner2
, and inner3
are all still private, as is the InnerObject
constructor. I just picked one for this.testFunc
to work on.