Reputation: 25
public function PubliCTest(_arg_1:MouseEvent):void {
trace("PubliCTestSub" + this.tell.text() + " , " + this.hello.text());
if (((this.istextValid()) && (this.istextsubValid()))){
var url:URLLoader = new URLLoader();
var req:URLRequest = new URLRequest("http://www.nasydbasda.com/tnewabsud.php");
var vars:URLVariables = new URLVariables();
vars.text1 = this.text1.text();
vars.textsub = this.textsub.text();
req.data = vars ;
req.method = URLRequestMethod.POST;
url.load(req);
url.addEventListener(Event.COMPLETE, reqcompleted);
}
this.onSignInSub();
}
This is my function, I want to take some functionality from it for use (another function in another class). Basically how do I call this from another class while keeping all the data (functions) from this current class.
Upvotes: 0
Views: 63
Reputation: 7316
Class methods are bound, they gonna stick to their own this object unless you forcibly pass them another one. That means, as long as you can refer this function of yours, you can call it from wherever.
Solution 1: singleton pattern. Assuming you have/need only one instance of the class with that method, you put the reference to the class static variable and can call it as YourClass.instance.yourMethod(args);
Solution 2: static reference. Basically, the same, with the same assumption there's only one class instance, you can bypass the instance reference and declare a public variable to address the method reference instead. You can call it as YourClass.MyMethod();
package
{
public class YourClass
{
static public var MyMethod:Function;
// Class constructor.
public function YourClass()
{
MyMethod = yourMethod;
}
public function yourMethod():void
{
// ... whatever
}
}
}
Solution 3: pass the reference. If you cannot go with singleton(-like) solution because you have several instances, well, then pass the method reference directly through your application architecture. You might want to read about dependency injection.
Upvotes: 1