name: duk_get_prop proto: | duk_bool_t duk_get_prop(duk_context *ctx, duk_idx_t obj_idx); stack: | [ ... obj! ... key! ] -> [ ... obj! ... val! ] (if key exists) [ ... obj! ... key! ] -> [ ... obj! ... undefined! ] (if key doesn't exist) summary: |
Get the property key
of a value at obj_idx
.
Return code and error throwing behavior:
1
is returned and key
is replaced by the property value on the value stack. However,
if the property is an accessor, the "getter" function may throw
an error.key
is replaced by undefined
on the value stack.obj_idx
is not
object coercible,
throws an error.obj_idx
is invalid, throws an error.The property read is equivalent to the Ecmascript expression
res = obj[key]
with the exception that the presence or absence of the
property is indicated by the call return value. For precise semantics, see
Property Accessors,
GetValue (V),
and [[Get]] (P).
Both the target value and the key
are coerced:
String
and you can access its
"length"
property.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.If the target is a Proxy object which implements the get
trap,
the trap is invoked and the API call always returns 1 (i.e. property present): the
absence/presence of properties is not indicated by the get
Proxy
trap. Thus, the API call return value may be of limited use if the target
object is potentially a Proxy.
If the key is a fixed string you can avoid one API call and use the
duk_get_prop_string()
variant.
Similarly, if the key is an array index, you can use the
duk_get_prop_index()
variant.