JS_New

This article covers features introduced in SpiderMonkey 1.8

Create an object as though by using the new keyword and a JavaScript function.

Syntax

JSObject *
JS_New(JSContext *cx, JS::HandleObject ctor, const JS::HandleValueArray& args); // Added in JSAPI 32

JSObject *
JS_New(JSContext *cx, JSObject *ctor, unsigned argc, jsval *argv); // Obsolete since JSAPI 32
Name Type Description
cx JSContext * The context in which to create the new object. Requires request. In a JS_THREADSAFE build, the caller must be in a request on this JSContext.
ctor JS::HandleObject The constructor function object.
args JS::HandleValueArray & An array of argument values to pass to the constructor. Added in SpiderMonkey 38
argc unsigned The number of arguments to pass to the constructor. Obsolete since JSAPI 32
argv jsval * Pointer to the element 0 of an array of argument values to pass to the constructor. If argc is 0, this may be NULL. Obsolete since JSAPI 32

Description

JS_New creates a new object as though by using the new operator, as described in ECMA 262-3 ยง11.2.2.

JS_New(cx, ctor, args) is equivalent to the JavaScript expression new ctor(...args), and JS_New(cx, ctor, argc, argv) is equivalent to the JavaScript expression new ctor(argv[0], argv[1], ... argv[argc-1]). If ctor is not an object that can be used as a constructor, a TypeError is raised.

On success, JS_New returns a pointer to the new object. On error, it returns NULL.

See Also