Consider the following code:
var App = function () {
this.interfaces = [];
};
App.prototype.user = {
create: function (name) {
this.interfaces.push(name);
}
};
var app = new App;
app.user.create('John Doe');
It'll unleash the following error:
Uncaught TypeError: Cannot read property 'push' of undefined
That's because this
, in user.create
has a different context than App
's one. Okay, but how can I access the property interfaces
from App
in user.create
?
Demonstration: JSFiddle