name: duk_new proto: | void duk_new(duk_context *ctx, duk_idx_t nargs); stack: | [ ... constructor! arg1! ...! argN! ] -> [ ... retval! ] summary: |

Call a constructor function with nargs arguments (not counting the function itself). The function and its arguments are replaced by a single return value. An error thrown during the constructor call is not automatically caught.

The target function this binding is set to a freshly created empty object. If constructor.prototype is an object, the internal prototype of the new object is set to that value; otherwise the standard built-in Object prototype is used as the internal prototype. The return value of the target function determines what the result of the constructor call is. If the constructor returns an object, it replaces the fresh empty object; otherwise the fresh empty object (possibly modified by the constructor) is returned. See [[Construct]].

example: | /* Assume target function is already on stack at func_idx. * Equivalent to ECMAScript 'new func("foo", 123)'. */ duk_idx_t func_idx = /* ... */; duk_dup(ctx, func_idx); duk_push_string(ctx, "foo"); duk_push_int(ctx, 123); duk_new(ctx, 2); /* [ ... func "foo" 123 ] -> [ ... res ] */ printf("result is object: %d\n", (int) duk_is_object(ctx, -1)); duk_pop(ctx); tags: - object - call seealso: - duk_pnew introduced: 1.0.0