I’m having some Javascript woes, lets say we have a constructor
function test(element) {
this.element = element;
this.element.onfocus = function() { this.onfocus(); }
}
test.prototype.onfocus = function() {
alert("focused");
}
new test(document.getElementByID("E1"));
So I am trying to dynamically set the focus event inside the constructor, but it doesn’t seem to work, and I can’t figure it out.
Anyone help?
Thanks,
AJ
In JavaScript, and in the context of
this.element.onfocus = function() {this.onfocus(); }, second this is the element who fired the event.In other words, your code should be like this if you want to work in a proper way:
Don’t think JavaScript is a decent object-oriented programming language, as it’s prototype-oriented and it has some weak things like one that’s affecting your code.
Why don’t you read this article? I belive it’ll clarify this keyword for you: