Molybden API
Loading...
Searching...
No Matches
molybden::JsObject Class Reference

A JavaScript object. More...

#include <js_object.hpp>

Inheritance diagram for molybden::JsObject:
molybden::JsArray

Public Types

using Private = JsObjectImpl
 

Public Member Functions

 JsObject (std::unique_ptr< Private > impl)
 
std::shared_ptr< molybden::Frameframe ()
 Returns the shared pointer to the Frame instance of this JavaScript object.
 
template<class T >
std::shared_ptr< T > as ()
 Returns the shared pointer of type T to this object if it represents one, or nullptr otherwise.
 
template<class... T>
JsReturnValue call (const std::string &method_name, T &&... args)
 Executes the method with the given method_name and the args in the JavaScript object.
 
template<class T >
bool putProperty (const std::string &name, T &&value)
 Creates a new property with the given name or updates the existing one in the current JavaScript object and returns true if the property with the given name was created or updated successfully.
 
bool removeProperty (const std::string &name)
 Removes a property with the given name in the JavaScript object and returns true if the property was successfully removed.
 
JsValue getProperty (const std::string &name)
 Returns the value of the JavaScript object's property with the given name.
 
std::vector< std::string > propertyNames ()
 Returns an immutable list of the names of the properties of this JavaScript object, including properties from the prototype objects.
 
std::vector< std::string > ownPropertyNames ()
 Returns an immutable list of the names of the properties of this JavaScript object, excluding properties from the prototype objects.
 
bool hasProperty (const std::string &name)
 Checks whether the JavaScript object has a property or function with the given name.
 
template<>
std::shared_ptr< JsArrayas ()
 
template<>
std::shared_ptr< JsObjectas ()
 

Protected Attributes

std::unique_ptr< Private > impl_
 

Detailed Description

A JavaScript object.

Allows accessing the object's properties and calling its functions. The JavaScript object is alive while the web page where the object was created is loaded. When the web page is unloaded, all the JavaScript objects will be automatically disposed. Access to the already disposed JavaScript object will have no effect.

Member Function Documentation

◆ as()

template<class T >
std::shared_ptr< T > molybden::JsObject::as ( )

Returns the shared pointer of type T to this object if it represents one, or nullptr otherwise.

Template Parameters
Tthe JsObject subclass type.

◆ call()

template<class... T>
JsReturnValue molybden::JsObject::call ( const std::string &  method_name,
T &&...  args 
)

Executes the method with the given method_name and the args in the JavaScript object.

This method blocks current thread execution until the function finishes its execution.

If the function raises an exception, then JsError with an error message that describes the reason of the exception will be returned. Same error message will be printed in JavaScript Console.

The type mapping rules are the following:

C++ JavaScript
double Double
std::string String
bool Boolean
std::shared_ptr<SomeJsObject> Object
std::shared_ptr<JsArray> Array
std::shared_ptr<SomeJsProxyObject> Proxy Object

where

Other types cannot be automatically converted to JavaScript values, and first must be converted to one of the types above.

Template Parameters
Tthe types of the arguments to pass, considered to be deduced.
Parameters
method_namethe name of the method.
argsarguments to pass to the JavaScript object method.
Returns
the result of the method execution.

◆ putProperty()

template<class T >
bool molybden::JsObject::putProperty ( const std::string &  name,
T &&  value 
)

Creates a new property with the given name or updates the existing one in the current JavaScript object and returns true if the property with the given name was created or updated successfully.

The type mapping rules are the following:

C++ JavaScript
double Double
std::string String
bool Boolean
std::shared_ptr<SomeJsObject> Object
std::shared_ptr<JsArray> Array
std::shared_ptr<SomeJsProxyObject> Proxy Object

where

Other types cannot be automatically converted to JavaScript values, and first must be converted to one of the types above.

Template Parameters
Tthe type of the object to set as a property value.
Parameters
namea string that represents the property name.
valuea new value set as the property with the given name.

◆ removeProperty()

bool molybden::JsObject::removeProperty ( const std::string &  name)

Removes a property with the given name in the JavaScript object and returns true if the property was successfully removed.

Once you remove the property, it will not be available in the current JavaScript object anymore.

Parameters
namea string that represents the name of the property or function.
Returns
true if the property was successfully removed.

The documentation for this class was generated from the following files: