call() function

The call() function is a generic method of any Function object. This function can be used in the following two scenarios:

1. Used as invokation to a callback function within another function;

2. if a function must be invoked as a method of an object, but is not actually stored as a method of that object ( I still don’t know how that could happen and I am interested in knowing)…

ActionScript:
public call(thisObject:Object, [parameter1:Object]) : Object

Usage:
function dosomeCallback(onReady:Function, scope, params):Void{
var x = 7;
var y = 5;
onReady.call(scope, params);
}
function saySomething(SomethingToSay:String):Void{
trace(SomethingToSay);
}
dosomeCallback(saySomething, null, “good morning!”);

About “thisObject”/first parameter in the function:

“In some situations, you might want thisObject to point somewhere else; for example, if a function must be invoked as a method of an object, but is not actually stored as a method of that object:

 

myObject.myMethod.call(myOtherObject, 1, 2, 3);

“You can pass the value null for the thisObject parameter to invoke a function as a regular function and not as a method of an object. For example, the following function invocations are equivalent:

 

Math.sin(Math.PI / 4)
Math.sin.call(null, Math.PI / 4)"

					
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: