I’m creating an Javascript “class” with prototyping. I don’t understand why the first/second block won’t work, and the third block will work. For the first/second block I get: “Object # has no method ‘validate’ “. Why does it do that, and is block 3 the correct way?
–Edit
I have tested this in Chrome/FF
–Edit2
If I call the Test prototype with:
var test = new Test();
And call the test var in de login prototype it will work….
Block 1
function Test(){
this.init();
}
Test.prototype.init = function(){
$(".login").click(this.login);
};
Test.prototype.login = function(event){
event.preventDefault();
this.validate();
console.log("login");
};
Test.prototype.validate = function(){
console.log("validate");
};
new Test();
Block 2
function Test(){
this.init();
}
Test.prototype.init = function(){
$(".login").click(this.login);
};
Test.prototype.login = function(event){
var self = this;
event.preventDefault();
self.validate();
console.log("login");
};
Test.prototype.validate = function(){
console.log("validate");
};
new Test();
Block 3
function Test(){
if(!(this instanceof LoginController)){
return new LoginController();
}
self = this;
this.init();
}
Test.prototype.init = function(){
$(".login").click(this.login);
};
Test.prototype.login = function(event){
event.preventDefault();
self.validate();
console.log("login");
};
Test.prototype.validate = function(){
console.log("validate");
};
new Test();
Here you’re attaching
this.loginas event handler. Done this way, the function will have it’sthisvalue reassigned by jquery to the element that triggered the event.To to keep a reference to the
thisvalue you actually want, try:or for browsers implementing
bind:or
jQuery.proxy, which does the same thing asbind:demo: http://jsbin.com/ilejiw/1/
Update: No, the 3rd variant is not the correct way, as every instance of
Testwould overwrite the same globalselfvariable. Soselfwould point to the last instance ofTest(as long as it has not been overwritten by something else).