I use SWFAddress for deep linking my site (link to SWFAddress). I like to break code into classes so I have a main structure similar to this:
function SomeClass() {
//this adds the this.handleChange() function to the
//SWFAddress event listener
this.initializeSWFA = function() {
//SWFAddress variable is instantiated in SWFAddress javascript file
//so I can use it here
SWFAddress.addEventListener(SWFAddressEvent.CHANGE, this.handleChange);
}
//SWFAddress suppose to call this function
this.handleChange= function(evt) {
//some code here
}
}
//instantiate the SomeClass
var someVar = new SomeClass();
someVar.initializeSWFA();
This line does not work here:
SWFAddress.addEventListener(SWFAddressEvent.CHANGE, this.handleChange);
I tried changing it to:
SWFAddress.addEventListener(SWFAddressEvent.CHANGE, this.handleChange());
or
var self = this;
SWFAddress.addEventListener(SWFAddressEvent.CHANGE, self.handleChange);
and these don't work either.
So how to reference a javascript function from a class in situation like this? If the function handleChange
would be outside of the class I can write the function's name.
Thank you in advance
EDIT
First, thank you for all the answers. I am still trying to figure how this all works in Javascript. I am not used to the object oriented model like here in Javascript.
This is the solution for now. I still can't figure out how to do this nicely in Javascript but this solution works. I tried to implement the solution suggested by ehudokai (thank you) however was not able to make it work.
function SomeClass() {
//this adds the this.handleChange() function to the
//SWFAddress event listener
this.initializeSWFA = function() {
//SWFAddress variable is instantiated in SWFAddress javascript file
//so I can use it here
SWFAddress.addEventListener(SWFAddressEvent.CHANGE, someFunc);
}
//SWFAddress suppose to call this function
this.handleChange= function(evt) {
//some code here
}
}
//instantiate the SomeClass
var someVar = new SomeClass();
function someFunc(evt) {
someVar.handleChange(evt);
}
someVar.initializeSWFA();
I don't like this because this involves defining one extra function, so takes extra space If anybody figures out how to add a method to SWFAddress EventListener from a Javascript object, please help me out.