name: duk_put_prop proto: | duk_bool_t duk_put_prop(duk_context *ctx, duk_idx_t obj_index); stack: | [ ... obj! ... key! val! ] -> [ ... obj! ... ] summary: |

Write val to the property key of a value at obj_index. key and val are removed from the stack. Return code and error throwing behavior:

The property write is equivalent to the Ecmascript expression:

  obj[key] = val
  

The exact rules of when a property write succeeds or fails are the same as for Ecmascript code making the above assignment.

For semantics, see Property Accessors, PutValue (V, W), and [[Put]] (P, V, Throw). Both the target value and the key are coerced:

In Ecmascript an assignment expression has the value of the right-hand-side expression, regardless of whether or not the assignment succeeds. The return value for this API call is not specified by Ecmascript or available to Ecmascript code: the API call returns 0 or 1 depending on whether the assignment succeeded or not (with the 0 return value promoted to an error in strict code).

If the key is a fixed string you can avoid one API call and use the duk_put_prop_string() variant. Similarly, if the key is an array index, you can use the duk_put_prop_index() variant.

example: | duk_bool_t rc; duk_push_string(ctx, "key"); duk_push_string(ctx, "value"); rc = duk_put_prop(ctx, -3); printf("rc=%d\n", (int) rc); tags: - property seealso: - duk_put_prop_string - duk_put_prop_index introduced: 1.0.0