This is because when you bind an event, the event handler function is called with the context of the DOM element that called event, the keyword thisrepresents the DOM element.
To get a bar, you must save the link to the external closure:
var bar = function() {
var self = this;
this.baz = function() {
this.input = $('.input');
this.input.bind("keydown keyup focus blur change", this.foo);
}
this.foo = function(event){
console.log(this);
console.log(self);
}
};
. bar new, this window baz foo , !
, :
var bar = {
baz: function() {
var input = $('.input');
input.bind("keydown keyup focus blur change", this.foo);
},
foo: function(event){
console.log(this);
console.log(bar);
}
};