javascript - How do I store an Object's functions pointers in an array? -


i trying store function pointers of object's functions in array.but it's giving me problems when want access property of object within function.could 1 solve or give me idea how work around it?

function o(){       this.name="hello";       this.f=function(){         alert(this.name);//why "this" refer array arr rather object?       };       this.arr=[];        this.arr["x"]=this.f;        }     var d=new o();     d.arr["x"](); 

in case, this reference object function called method of (in case, array). you'll want store reference o function somewhere within scope, such as:

function o(){       var self = this;       this.name="hello";       this.f=function(){         alert(self.name);//why "this" refer array arr rather object?       };       this.arr=[];        this.arr["x"]=this.f;        }     var d=new o();     d.arr["x"](); 

this pretty common pattern in javascript, , has added benefit of allowing function execute same way, whether called through d.f() or d.arr["x"]()


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -