NAME

JSPL::Function - Reference to a JavaScript Function

DESCRIPTION

Functions in JavaScript are actually Function-objects. This class encapsulates them and allows you to invoke them from Perl.

The basic way to invoke a JSPL::Function instance is with the method "call" in JSPL::Context.

    my $func = $ctx->eval(q{
	// Define a simple function
	function myfunc(arg1) {
	    say("You sendme " + arg1);
	};
	// And return a reference to it
	myfunc;
    });

    $ctx->call($func => "some text"); # say 'You sendme some text'

You can use $ctx->call with the name of the Function as its first argument, but a JSPL::Function instance can hold a reference to an anonymous one:

my $func2 = $ctx->eval(q{ function (a, b) { return a + b }; });
$ctx->call($func2 => 5, 6);         # 11
$ctx->call($func2 => "foo", "bar"); # 'foobar'

Instances of JSPL::Function implement a short cut to avoid such verbose code:

$func2->(10, 20);     # 30
$func->('a value');   # Say "You sendme a value"

Please read on.

This class inherits from JSPL::Object.

PERL INTERFACE

Function instances are JavaScript Objects and as such, they have some methods, and this module adds some more, usable from perl.

INSTANCE METHODS

call ( $this, ... )
$func->call($somethis, $arg1, $arg2);

Call the underlaying JavaScript Function as an instance of the $somethis argument. All remaining arguments are passed as arguments to the function.

That is, inside the function this will be the value of $somethis.

This is the analogous to func.call(somethis, arg1, arg2) in JavaScript when func is a reference to the function to be called.

This is different from $ctx->call($func, ...) that always uses the global object for this.

apply ( $this, $array_arguments )
$func->apply($somethis, \@arguments);

Call the underlaying JavaScript Function in the same way as "call" above, but use the elements of $array_arguments as the arguments to the call, $array_arguments must be an ARRAY reference.

Analogous to func.apply(somethis, arguments) in JavaScript.

new ( )

Call the underlaying JavaScript Function as a constructor.

prototype ( )

Returns the prototype of the function as a JSPL::Object. Useful if the function is a constructor and you need to inspect or modify its prototype property.

CODE_REF ( )

Returns a CODE_REF that encapsulate a closure that calls the underlaying JSPL::Function.

The reference is cached, so every time you call CODE_REF, you obtain the same reference. This reference is the same one used for the "OVERLOADED OPERATIONS" below, so you seldom need to call this method.

INSTANCE PROPERTIES

All instances of Function have a few properties which can be used in Perl when the JSPL::Function is seen as a JSPL::Object

name
$func->{name}; # 'myfunc'

Retrieves the name of the function.

length
$func->{length}; # 1

Retrieves the number of arguments that the function expects.

OVERLOADED OPERATORS

Instances of this class overload &{} which means that you can use the instance as a code-reference directly.

$func->($arg1, $arg2, ...);

And inherit the overload %{} from "OVERLOADED OPERATORS" in JSPL::Object.