Reputation: 415
I am having a hard time figuring out why I am getting this error. I made sure that the JavaScript is loaded after the html is, I have even tried replacing this.doc and this.win with just document and window and it has not changed the result. Here is the code I am working with. It is implemented in <head>
through <script src="window.js" type="text/javascript"></script>
(I have also tried it at the end of the doc)
Window = function(doc, win)
{
this.doc = doc;
this.win = win;
this.resize();
this.win.addEventListener('resize', this.resize);
}
Window.prototype =
{
resize: function()
{
this.doc.body.innerHTML = window.innerHeight;
}
};
window.onload = new Window(document, window);
Any help would be much appreciated.
Upvotes: 0
Views: 2108
Reputation: 382274
That's because this
isn't the Window
object when the callback is called but the global object (window
). A solution would be to bind the function :
this.win.addEventListener('resize', this.resize.bind(this));
If you really need to be compatible with IE8, do this :
var _this = this;
this.win.addEventListener('resize', function(){_this.resize() });
But your global construct raises alarms. Be careful not to build useless verbose classes "java-style". Do you really need this Window
class ?
Upvotes: 3