Search code examples
javascriptprototype-programmingserverside-javascript

Can a callback function that belongs to a JavaScript object prototype access the object members?


How can callback function that belong to a JavaScript object prototype access the object members? the callback can't be closure, everything must be defined as follows:

function Obji(param){
   this.element = param;
}

Obji.prototype.func(){
   database.get("someKey",this.cb);
}

Obji.prototype.cb(){
   //here I would like to access this.element
}

Solution

  • database.get("someKey",this.cb.bind(this));

    .bind, ES5 shim for older browsers