The point is that a function must evaluate to the "parent" function. So e.g.
foo().bar().test();
has to evaluate to:
foo().test();
so that you can call another function on foo()
. To do this, you can return this
:
function foo() {
// empty, nothing interesting here
}
foo.prototype.bar = function() {
return this;
}
foo.prototype.test = function() {
return this;
}
Then,
var something = new foo();
something.bar() === something; // true
And because of this:
something.bar().test() === something.test(); // true
So because something.bar()
evaluates to something
, you can immediately call the second function in one go.