The bind
is placed wrong. You want to bind this
to the event handler function, not to the jQuery collection of objects which are returned from the ‘on’ method.
Following sample should work:
$('#btn').on('click', function(event){
console.log(this.test);
}.bind(this));
However, it’s not very good practice. Better approach could be to reference the this
object by some private variable. Common practice is to cache the this
object into some local variable called self, that, _this or .. etc. For example you can use following approach:
var self = this;
$('#btn').on('click', function(event){
console.log(self.test);
});
solved Binding this? Getting undefined?