views:

35

answers:

1

Below is my code for FF Extension monitoring browsing behaviour. I can't access trim method from processClick method handling click event.

Console shows this.trim is not a function. I know that it maybe something with this scope.

I will be really grateful for any help.

function bbm(doc)
{
    this.doc = doc;

    this.registerListeners = function() {
        this.doc.addEventListener("click", this.processClick, false);
    };

    this.trim = function(str)
    {
        return str.replace(/^\s+|\s+$/g, '') ;
    };


    this.processClick = function(e) {
        alert(e.type + " " + this.trim(this.url));
    };

};
A: 

Use this function

Function.prototype.bind = function(obj) {   
    var _method = this;
    return function() {
        return _method.apply(obj, arguments);
    };    
} 

Then,

this.registerListeners = function() {
     this.doc.addEventListener("click", this.processClick.bind(this), false);
};
unigg
Thanks a lot it works perfectly.
raptorek