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:
1
.obj_index
is not
object coercible,
throws an error.obj_index
is invalid, throws an error.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:
key
argument is internally coerced to a string. There is
an internal fast path for arrays and numeric indices which avoids an
explicit string coercion, so use a numeric key
when applicable.
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.