What is the difference between calling exampleFunction() and exampleFunction.call() ?
There is a feature on Actionscript that allows you to use functions through the ‘call’ method. For example, to use the ‘test’ function below, I could write test() and test.call()
function test():void {
trace('function was called')
}
What is the difference?
As with ECMAScript (from which AS is derived) it allows specifying the “this” context via the first parameter.
See the Function.call documentation. Note the remarks about functions vs. methods – or “bound functions” – and the examples.
That is, given:
Then:
Both
f.call(x)andx.f()evaluate toxwhilef()does not as it has a different “this” (it evaluates toGlobalinstead).If
f.call()(no params) is used then “this” will be NaN whilef(), as per above, hasGlobalas “this”. Try usingtrace(this)in the test code as it will yield more useful contextual information.Happy coding.