I am trying to create a "class" in JS, a simplified structure of which is below:
http://codepen.io/Deka87/pen/WpqYRP?editors=0010
function Alert() {
this.message = "Test alert";
this.document = $(document);
this.document.on('click', function() {
this.show();
}.bind(this));
};
Alert.prototype.show = function() {
setTimeout(function() {
console.log(this.message);
}, 50);
};
var alert = new Alert();
When you click on the document
it should show you the this.message
contents in console. However, it is now shown as undefined
. I believe the problem is that this.messsage
can't get the original this
context because it is wrapper in another function (setTimeout
in my case). Any help would be appreciated!