How do I access the methods of a dynamically created movieclip/object?
For simplicity sake I didn't post code on how I dynamically created the movieclip. Instead, assume its already created. It is an object. It is called field_2. Below it is referenced by using getChildByName('field_' + field.id);
Check_box_component.as
public var testVar:String = 'test';
public function testReturn()
{
return 'value returned';
}
Main.as
var temp:MovieClip = MovieClip(getChildByName('field_' + field.id));
trace(temp);
trace(temp.testReturn);
trace(temp.testVar);
Output:
[object Check_box_component]
function Function() {}
test
When I trace temp.testReturn, why does it show "function Function() {}" instead of "value returned"?
This link below helped me get this to this point.
http://curtismorley.com/2007/06/13/flash-cs3-flex-2-as3-error-1119/
have you tried:
trace(temp.testReturn());
... instead of your
trace(temp.testReturn);
... ?
I think you will have the result you are waiting for.
Actually, when doing "temp.testReturn", you are not calling the function. You need to add the parenthesis to make the actual call.
When you make a trace of temp.testReturn, the function is not executed: the trace function tell you the type of temp.testReturn, which is here correctly returned as a "function" type.
There is a difference between a function reference and a function call. Parenthesis '()' are an operator sign of ActionScript. They tell the compiler "please try to make a call to what was just behind us". Or at least I hope they are that polite.
A function in ActionScript is an object, like all other stuff. A member of Function class. You can pass it's reference back and forth, you can even call it's methods like call() or apply().
If you want a call, and not a reference, you have to use call operator.
trace(temp.testReturn());
EDIT You accepted an answer while I was typing, sorry for a duplicate answer.
Related
I read this : http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Function.html
But that does not exactly correspond to what I want to do, and I can't find the right solution. However my question is not so complicated.
Here is the situation :
mains.as contains a functionA(strParam:String)
onlineClass.as contains a functionB working like this :
private static functionB (fnParam:Function):void //my fnParam is functionA
{
var strParam:String = getSomeStringResult();
//I have a result from a function
fnParam.call(strParam);
//I want to execute functionA with strParam as parameter
}
But I don't understand what I have to do with call parameters.
I tried :
fnParam.call(null, strParam);
But it returns an error :
[Fault] exception, information=TypeError: Error #1009: Impossible to access a property or a method of a null object's reference
I am sure the answer already exists somewhere but a search with "function" and "call" leads nowhere.
Thank you for the help.
If a parameter is a function then you can call it directly as the parameter name if it's being passed to another function:
functionB(fnParam:Function){
fnParam('strParam');
}
The issue here looks like like the functionA is null when it's being passed through to functionB. This is possibly because they're in different files / classes but you'll probably want to debug before functionB is called to make sure functionA is accessible.
Ok, I made my code working, and I think it could be a static story. FunctionA is not static, and when I pass it as a parameter of the static FunctionB, this works :
private static var functionACallback:Function;
public static functionB( functionA:Function ):void
{
functionACallback = functionA; //set the static functionACallback
var strParam = getSomeStringResult(); //get the string
functionACallback(strParam); //call the static var with parameter
}
I don't entirely understand the issue here, but the above code resolve the problem.
defenestrate.me's answer was helpful.
In this example:
public function Roulette() {
new QuickLoad(url, function (o:*):void {trace(this);});
}
when QuickLoad instance does its stuff, it calls the anonymous function. One would think that this is Roulette. But no, it turns out to be the anonymous function's caller, which is QuickLoad.
This is weird to say the least, say how am I supposed to pass the "correct" this (i.e. Roulette instance) inside the anonymous function if I don't do it the normal way?
Just save the outer this instance under a different name so that it is preserved:
public function Roulette() {
var rouletteThis = this;
new QuickLoad(url, function (o:*):void {trace(rouletteThis);});
}
There is a way to call a function with an alternate this pointer, but since your function is called from within new QuickLoad(), you need to alter that call statement, and pass your this as Roulette into the constructor. Your new QuickLoad object is unaware of its surroundings, and even the caller of the constructor is unknown to it. Thus, you need to make it aware, pass a this pointer from Roulette() to QuickLoad(), AND call the function from QuickLoad with passing an alternate this pointer.
public function QuickLoad(url:String,caller:Object=null,callback:Function=null) {
// initialization code
if (callback!=null) {
if (caller!=null) callback.apply(caller,[o]);
else callback.apply(this,[o]);
}
}
...
public function Roulette() {
new QuickLoad(url, this, function (o:*):void {trace(this);});
}
Function::apply() manual.
You can also use call() method, if your argument array has fixed length. callback.call(caller,o);
Generally, in this context, this refers to an object. To quote a rather infamous acronym: INABIAF (It's not a bug, it's a feature), LOL. So, yes, the object instance QuickLoad that is calling the function is going to be what this looks at by default.
There is an exception I know of (out of many, I'm sure)...you can get anything...variable, function, object, whatever, via this["Name of Object"]. But that's an aside.
There ARE other workarounds, I'm sure, which may or may not be practical for your purposes. This is one way of passing a function, out of many, and it's the one I use the most.
Functions do not have instances. They're not objects. If you want to send a function as an argument to another function, you simply pass it, as follows in this rather weird example.
//This function accepts a function as an argument.
function bridgeOfQuestions(person:String, response:Function):void
{
if(person == "King Arthur")
{
response("What is the average airspeed velocity of an unladen swallow?");
}
else
{
response("What is your favorite color?");
}
}
//This is the function we're going to pass.
function askQuestion(question:String):void
{
trace(question);
}
//Here, we call bridgeOfQuestions and pass it the askQuestion function.
//NOTE: Leave off the parenthesis on the function being passed!
bridgeOfQuestions("Sir Lancelot", askQuestion);
bridgeOfQuestions("King Arthur", askQuestion);
EDIT: If it is just the name you're passing, a function is a function permanently. It doesn't change, unlike an object, and as I said, it doesn't have instances. Therefore, if you merely want to print out the name of the function, you'd only use trace("Roulette").
I would like to call a function name from inside an addEventListener dynamically based on function parameter.
calling with newMod("moduleA", "A"); however I am getting error TypeError: Error #1006: value is not a function.
Any suggestions on how I can call this function dynamically. I have seen some answers around using an instance[function]() but am not sure how that applies with the listener,
public function newMod(mdLd,evtTyp,param):void {
info = ModuleManager.getModule(mdLd);
var mevth:String = ("modEventHandler"+(evtTyp));
info.addEventListener(ModuleEvent.READY, function(e:ModuleEvent){
this[mevth](e, param)});
info.load(null, null, null, moduleFactory);
}
private function modEventHandlerA(e:ModuleEvent):void {
vg1.addElement(info.factory.create() as IVisualElement);
}
[EDIT]
looks like changing the call to this[mevth]() works, but I cant seem to pass additional params as needed i.e. this[mevth](parm), any suggestions welcome.
I have also updated the listener to include a function call but still no joy
When you create closure "this" doesn't point to real instance "this". You can write something like this
public function newMod(mdLd,evtTyp,param):void {
info = ModuleManager.getModule(mdLd);
var self:Object = this;
var mevth:String = ("modEventHandler"+(evtTyp));
info.addEventListener(ModuleEvent.READY, function(e:ModuleEvent){
self[mevth](e, param)});
info.load(null, null, null, moduleFactory);
}
...
But I really don't recommend you write code like this.
To awnser your comment:
Function closure means that a function remembers the context in which it has been created. In your code the following line creates a closure:
info.addEventListener(ModuleEvent.READY, function(e:ModuleEvent){
this[mevth](e, param)});
The event handler function is created in the context of the class that contains the method public function newMod(mdLd,evtTyp,param):void, so your handler has access to all members of the class. This includes variables declared in the surrounding method, the private variables/methods of the class and the protected variables/method in the whole inheritence chain of the class.
The problem with the this as Ivan Dyachenko wrote is that your handler function actually is an instance of the top-level class Function. So, if you try to call a method on this in a Function object it will be the same as calling a method in any other object - this refers to the object. In your case this will be the Function object. But your function doesn't have the method you want to call on it.
Additionally you will run into another problem with your code. Because you create the event handler inline - directly as argument of addEventListener() you will be unable to remove the event listener later. You should either use the useWeakReference parameter on addEventListener() as described here or store the handler function in a variable to hold a reference on it to remove it later with removeEventListener().
In as3 there is a flexible way to change object instance, when calling it.
call or apply members of Function object can be called with specific first arg, and reference say us, that this first arg will be "this" pointer inside function. But i've found it wrong.
I'v write little test, listed below.
public class Test
{
private var name:String = "default";
public var test3:Function = test;
public var test2:Function = function()
{
trace(this.name);
}
public function Test(name:String)
{
this.name = name;
}
public function test():void
{
trace(this.name);
}
}
and tested it.
var tmp:Test = new Test("default");
tmp.test(); //out default
tmp.test.call(new Test("new")); //out default
tmp.test2(); //out default
tmp.test2.call(new Test("new2")); //out new2
tmp.test3(); //out default
tmp.test3.call(new Test("new3")); //out default
So, in anonymous function call we can get right output, but not in case of member function.
maybe it's becouse of ambiguous "this" pointer, that should reffer real object instance for correct work, maybe smth else. I dont now, and as3 reference didnt't describe smth about it.
Finally list of questions:
Why so? By me, it's very strange, and looks like undefined behaviour;
How i can achieve that functionality? How to deceive test function like anonymous one? Isn't it call methode target?
It isn't very important, but I'll be glad any good answer. Thanks!
P.S. sorry for my English.
//EDITED: added this statement to all "name" references. Nothing changes.
When invoking the [[Call]] property, the behavior is different for
different types of closures. A closure is an object that contains a
reference to a method, and the [[Call]] property acts differently
depending on whether it is a function, method, or class closure. A
function closure is one that is of a global method that isn't
associated with any instance of a class. A method closure contains an
instance method of a class, and will always remember its original
"this" value.
If the closure is a function closure, then the first argument passed
to [[Call]] is passed on to the method and gets used as the "this"
value. If the first argument is null or undefined, then the global
object will be used as the "this" value for the method.
If the closure is a method closure, then the first argument of
[[Call]] will be ignored, and the saved "this" value for the method
closure will be passed to the method as the first argument. A method
closure records what its original "this" value was and always uses
that instead of the first argument to [[Call]].
If the closure is a class closure, and there is 1 argument passed to
[[Call]] (in addition to the "this" argument), then the call is
treated as a type conversion, and the argument will be coerced to the
type represented by the closure.
http://learn.adobe.com/wiki/display/AVM2/2.4+Method+invocation+notes
long shot: is it possible to get the name of a calling function or the constructor from the called function? is it possible to determine the previous function of the thread?
i would like to call some setter functions from my constructor and have my setter functions determine if it was the constructor that called them.
currently, i'm setting a boolean for this functionality, but perhaps there is another way?
public function Constructor(myNumber:Number)
{
this.myNumber = myNumber;
}
public function set myNumber(value:Number):void
{
myNumberProperty = value;
//if constructor called this, return;
//else do some other stuff;
}
Quote from liveDocs:
Unlike previous versions of ActionScript, ActionScript 3.0 has no arguments.caller property. To get a reference to the function that called the current function, you must pass a reference to that function as an argument. An example of this technique can be found in the example for arguments.callee.
It was in AS2.0... It unfortunately throws an error if done in AS3.0.
Technically, you should be able to do this by generating an error and getting its stack trace. The constructor will have to be on that stack trace.
try
{
throw new Error();
}
catch (e:Error)
{
// parse this for the constructor name
trace(e.getStackTrace());
}
That would be for detecting where a function call came from...
I would still go for your solution (setting the flag), as it's more oop and probably far faster in terms of performance.