jason
jason

Reputation: 1631

Function call(thisArg:*, ... args) first parameter usage in actionscript

what does call(thisArg:*, ... args) first parameter mean?

Assuming f() is defined in a unnamed package as global function, following is the code snippet:

package {
    public function f(message:String):void {
        trace(message);
        trace(this.watchedValue);
    }
}

test code as following:

public function test():void {
    var obj:Object = {watchedValue:100};
    f("invoking f");
    f.call(obj, "invoking f by call()");//actual result is undefined, but shouldn't be 100?
}

Upvotes: 0

Views: 177

Answers (2)

Aquahawk
Aquahawk

Reputation: 370

This param only used in closures and anonymous functions, like

var testFunc:Function = function():void{trace(this.watchedValue)}

EDIT: in you case it will be

package {
    public var f:Function = function(message:String):void {
        trace(message);
        trace(this.watchedValue);
    }
}

EDIT2 first parameter of call will be this in called function. This is the way to call fauction like a method of object. But when function is method or top level function first parameter of call() will be ignored. To use first param your function must be variable with anonymous function.

Upvotes: 1

Mark Knol
Mark Knol

Reputation: 10143

As far as I know Function.call() is the same as function(), except the fact you change the scope of this. Normally this referrers to the current class, but it could be another class. \

Your test function looks wrong, it should be obj instead of o

public function test():void {
    var obj:Object = {watchedValue:100};
    f("invoking f");
    f.call(obj, "invoking f by call()");
}

Upvotes: 0

Related Questions