JSObjectProperty Property (String)Awesomium.NET 1.7.6Awesomium.NET
Gets or sets the value of the specified named property.

Namespace: Awesomium.Core
Assembly: Awesomium.Core (in Awesomium.Core.dll) Version: 1.7.6.0
Syntax

public JSValue this[
	string propertyName
] { get; set; }

Parameters

propertyName
Type: SystemString
The name of the property whose value will be set or retrieved. If the property being set does not exist, it will be created.

Return Value

Type: JSValue
A JSValue representing the value of the specified named property.
Exceptions

ExceptionCondition
InvalidOperationException Creating a Named Accessor Property, is only supported on valid Remote objects.
InvalidOperationException You cannot set a property synchronously from inside a synchronous Javascript Execution Context (see: Javascript Execution Context)
Remarks

This property can be used to create or set a named data property, or to create, set and configure a Named Accessor Property.
Caution note Caution
Creating a Named Accessor Property, is only supported on valid Remote objects.
By default, setting this property to any value other than a JSPropertyDescriptor instance, creates or sets a regular named data property. To create a named accessor property, follow the steps below:
  1. Create and appropriately fill an instance of JSPropertyDescriptor, representing the property descriptor of the accessor property. (Also read: Property Attributes for how to fill the JSPropertyDescriptor instance to define either an accessor property or a data property.)
  2. Specify the name of the property in the propertyName parameter.
  3. Assign the JSPropertyDescriptor instance as value to this property. (See the Example section below.)
To create a configured named data property, it is suggested that you use any of the following overloads:

The values assigned or retrieved from, this property, are encapsulated by a JSValue. JSValue supports explicit and implicit casting to all data types available with the Javascript Integration API.

Caution note Caution
Trying to retrieve the value of a property from inside a synchronous Javascript Execution Context, will return Undefined.

Trying to synchronously set the value of a property from inside a synchronous Javascript Execution Context, will throw an InvalidOperationException.

For details, read: Synchronous & Asynchronous JavaScript API. article.

Examples

The following examples demonstrate creating named data and accessor properties:
webView.CreateJavascriptExecutionContext( onJavaScript );

[...]

private void onJavaScript( Global global )
{
    if ( !global )
        return;

    // Create a local JSObject.
    JSObject myObj = new JSObject();
    // Create a Named Data Property.
    myObj[ "name" ] = "Bob";
    // Pass our object to the remote page.
    // Named Accessor Properties are only
    // supported on remote objects.
    // (Note that all members of Global
    // are provided as Dynamic.)
    global.window.myObject = myObj;

    // Create an accessor property descriptor.
    JSPropertyDescriptor desc = new JSPropertyDescriptor( true )
    {
        Enumerable = true,
        Get = new JSFunction( webControl, (JSFunctionHandler)getAge ),
        Set = new JSFunction( webControl, (JSFunctionAsyncHandler)setAge )
    };
    // Create a Named Accessor Property.
    global.window.myObject.age = desc;
}

[...]

private JSValue age;

private JSValue getAge( JSValue[] args )
{
    // You may need to apply some app-specific
    // coercing here.

    return age;
}

private void setAge( JSValue[] args )
{
    // Apply some type checking.
    if ( ( args.Length < 1 ) || !args[ 0 ].IsNumber )
        return;

    age = args[ 0 ];
}
JavaScript
// Will call our managed |getAge| getter.
var testAgeValue = window.myObject.age;
// Will call our managed |setAge| setter.
window.myObject.age = 'someString';
// |testAgeValue| will be unchanged because our
// managed setter applies type checking.
testAgeValue = window.myObject.age;
// This will successfully change the value.
window.myObject.age = 35;
Below, a similar example demonstrating full usage of the DLR support:
private void onJavaScript( Global global )
{
    if ( !global )
        return;

    // Anonymous Classes supported and converted
    // to local JSObjects.
    global.window.myObject = new { name = "Bob" };

    // Our accessor property descriptor as an
    // Anonymous Class with delegates passed
    // directly (no need to create JSFunction
    // objects).
    var desc = new
    {
        enumerable = true,
        get = (JSFunctionHandler)getAge,
        set = (JSFunctionAsyncHandler)setAge
    };
    // Use the |defineProperty| of JavaScript's global |Object|.
    global.Object.defineProperty( global.window.myObject, "age", desc );
}
See Also

Reference