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:

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:

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.

example: | /* reading [global object].Math.PI */ duk_push_global_object(ctx); /* -> [ global ] */ duk_push_string(ctx, "Math"); /* -> [ global "Math" ] */ duk_get_prop(ctx, -2); /* -> [ global Math ] */ duk_push_string(ctx, "PI"); /* -> [ global Math "PI" ] */ duk_get_prop(ctx, -2); /* -> [ global Math PI ] */ printf("Math.PI is %lf\n", (double) duk_get_number(ctx, -1)); duk_pop_n(ctx, 3); /* reading a configuration value, cfg_idx is normalized * index of a configuration object. */ duk_push_string(ctx, "mySetting"); if (duk_get_prop(ctx, cfg_idx)) { const char *str_value = duk_to_string(ctx, -1); printf("configuration setting present, value: %s\n", str_value); } else { printf("configuration setting missing\n"); } duk_pop(ctx); /* remember to pop, regardless of whether or not present */ tags: - property seealso: - duk_get_prop_string - duk_get_prop_index introduced: 1.0.0