Although it is somewhat simplistic, when you execute the following code, this points to the document and you can not get an instance of Hoge.
If you make variables and functions global, there is no meaning defined in the closure, and is there any better way?
var Hoge = function() {
this.hogehoge = 'hogehoge';
document.addEventListener('mousedown', this.change, false);
};
Hoge.prototype = {
change: function(e) {
e.pageX
console.log(this.hogehoge); // undefined
}
};
let hoge = new Hoge();
I expect the output of console.log
to be 'hogehoge'
, but the actual output is undefined
.