Skip to main content

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 context
  • ptab - Pointer to receive the property array (must be freed with JS_FreePropertyEnum())
  • plen - Pointer to receive the array length
  • obj - The object to enumerate
  • flags - 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 context
  • desc - Pointer to receive the property descriptor
  • obj - The object
  • prop - The property name atom

Returns

Returns:
  • 1 if the property exists
  • 0 if the property doesn’t exist
  • -1 on 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).
Returns 1 if the property exists, 0 if not, -1 on error.

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_ONLY to get only enumerable properties (like Object.keys())
  • Combine flags with | to enumerate multiple types of properties