Property Operations
QuickJS provides APIs to enumerate object properties and work with property descriptors.Property Enumeration
JS_GetOwnPropertyNames
Get an array of an object’s own property names.Parameters
ctx- The JavaScript contextptab- Pointer to receive the property array (must be freed withJS_FreePropertyEnum())plen- Pointer to receive the array lengthobj- The object to enumerateflags- Enumeration flags
Enumeration Flags
Returns
Returns 0 on success, -1 on error.Example
JSPropertyEnum Structure
JS_FreePropertyEnum
Free a property enumeration array.Property Descriptors
JSPropertyDescriptor Structure
Property Flags
JS_GetOwnProperty
Get the property descriptor for an own property.Parameters
ctx- The JavaScript contextdesc- Pointer to receive the property descriptorobj- The objectprop- The property name atom
Returns
Returns:1if the property exists0if the property doesn’t exist-1on error
Example
Defining Properties
JS_DefineProperty
Define a property with full control over its attributes.Example
JS_DefinePropertyValue
Define a simple data property.JS_DefinePropertyGetSet
Define a property with getter and/or setter.Property Testing
JS_HasProperty
Check if an object has a property (including inherited).Example
Notes
- Always free property enumeration arrays with
JS_FreePropertyEnum() - Property descriptor values must be freed if they are not
JS_UNDEFINED - Use
JS_GPN_ENUM_ONLYto get only enumerable properties (likeObject.keys()) - Combine flags with
|to enumerate multiple types of properties