The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

JE::Object - Base class for all JavaScript objects

SYNOPSIS

  use JE;
  use JE::Object;

  $j = new JE;

  $obj = new JE::Object $j;

  $obj->prop('property1', $new_value);  # sets the property
  $obj->prop('property1');              # returns $new_value;
  $obj->{property1} = $new_value;       # or use it as a hash
  $obj->{property1};                    # ref like this

  $obj->keys; # returns a list of the names of enumerable property
  keys %$obj;

  $obj->delete('property_name');
  delete $obj->{property_name};

  $obj->method('method_name', 'arg1', 'arg2');
    # calls a method with the given arguments

  $obj->value ;    # returns a value useful in Perl (a hashref)

  "$obj";  # "[object Object]" -- same as $obj->to_string->value
  0+$obj"; #  nan -- same as $obj->to_number->value
  # etc.

DESCRIPTION

This module implements JavaScript objects for JE. It serves as a base class for all other JavaScript objects.

A JavaScript object is an associative array, the elements of which are its properties. A method is a property that happens to be an instance of the Function class (JE::Object::Function).

JE::Object objects can be used in Perl as a number, string or boolean. The result will be the same as in JavaScript. The %{} (hashref) operator is also overloaded and returns a hash that can be used to modify the object. See "USING AN OBJECT AS A HASH".

See also JE::Types for descriptions of most of the methods. Only what is specific to JE::Object is explained here.

METHODS

$obj = JE::Object->new( $global_obj )
$obj = JE::Object->new( $global_obj, $value )
$obj = JE::Object->new( $global_obj, \%options )

This class method constructs and returns a new JavaScript object, unless $value is already a JS object, in which case it just returns it. The behaviour is the same as the Object constructor in JavaScript.

The <%options> are as follows:

  prototype  the object to be used as the prototype for this
             object (Object.prototype is the default)
  value      the value to be turned into an object

prototype only applies when value is omitted, undef, undefined or null.

To convert a hash into an object, you can use the hash ref syntax like this:

  new JE::Object $j, { value => \%hash }

Though it may be easier to write:

  $j->upgrade(\%hash)

The former is what upgrade itself uses.

$j->new_function($name, sub { ... })
$j->new_function(sub { ... })

This creates and returns a new function object. If $name is given, it will become a property of the object. The function is enumerable, like alert et al. in web browsers.

For more ways to create functions, see JE::Object::Function.

$j->new_method($name, sub { ... })
$j->new_method(sub { ... })

This is the same as new_function, except that the subroutine's first argument will be the object with which the function is called, and that the property created will not be enumerable. This allows one to add methods to Object.prototype, for instance, without making every for-in loop list that method.

For more ways to create functions, see JE::Object::Function.

$obj->typeof

This returns the string 'object'.

$obj->class

Returns the string 'Object'.

$obj->value

This returns a hash ref of the object's enumerable properties. This is a copy of the object's properties. Modifying it does not modify the object itself.

2 POD Errors

The following errors were encountered while parsing the POD:

Around line 90:

=over without closing =back

Around line 583:

'=end to delete' is invalid. (Stack: =over; =begin to)