Page tree
Skip to end of metadata
Go to start of metadata

Exposing simple values to JavaScript

If you need to expose a simple value to JavaScript that doesn't require any special logic when it is queried, a Property may be overkill.  That is why FB::JSAPIAuto provides Attributes.

Attributes work just like properties, but instead of having a getter and setter that are called by javascript they have a simple value.  There are two types of Attributes:

  • Read/Write Attributes - These can be read and set from either JavaScript or C++, but no special function calls occur when they are accessed.
  • Read-only Attributes - These can only be set from C++ and can be read from anywhere.

Setting the value of a read/write attribute

Setting (or registering) an attribute value is simple and can be done from any function at any time (not guaranteed to be thread-safe). You just need to call the registerAttribute method.

registerAttribute("name", "Default");

Once an attribute is registered like this, you can access it from JavaScript – or even change it.

Setting the value of a read-only (constant) attribute

Probably most often when we use attributes we don't want it to be changeable from JavaScript. Note that even though we call this a read-only attribute, it can still be changed from C++ – just not from JavaScript.

registerAttribute("MAX_SIZE", 20, true);

Note that attributes can be any type supported by FireBreath.

  • No labels

1 Comment

  1. Unknown User (pnd1305)

    在JSAPI对象预定属性:registerAttribute("name", "Default"),可读写;registerAttribute("name", "Default",true)只读.属性可包含任何类型。