J
Jeff Bigham
Hi,
Consider the following simplified code in which Foo is used as a
'namespace':
var Foo = {
doAlert: function() {
alert('hello');
},
myEventHandler: function(e) {
this.doAlert();
}
}
document.addEventListener('keydown', Foo.doAlert, false);
This gives me an error because when responding to the event, the
object 'this' is the document because that's what I've attached the
event handler to. I know I could just replace 'this' with "Foo.' but
my understanding is that I take a performance hit every time I make
such a global reference. And, since I'm developing a large web
application in Javascript, I'd like to avoid unnecessary performance
hits when possible.
Anyone know what is the accepted standard of what to do in this case?
Thanks!
Jeff
Consider the following simplified code in which Foo is used as a
'namespace':
var Foo = {
doAlert: function() {
alert('hello');
},
myEventHandler: function(e) {
this.doAlert();
}
}
document.addEventListener('keydown', Foo.doAlert, false);
This gives me an error because when responding to the event, the
object 'this' is the document because that's what I've attached the
event handler to. I know I could just replace 'this' with "Foo.' but
my understanding is that I take a performance hit every time I make
such a global reference. And, since I'm developing a large web
application in Javascript, I'd like to avoid unnecessary performance
hits when possible.
Anyone know what is the accepted standard of what to do in this case?
Thanks!
Jeff