JS_NewObjectForConstructor

Convenience function to create a new object exactly as a constructor function would.

Syntax

JSObject *
JS_NewObjectForConstructor(JSContext *cx, const JSClass *clasp, const JS::CallArgs& args); // Added in JSAPI 32

JSObject *
JS_NewObjectForConstructor(JSContext *cx, JSClass *clasp, const jsval *vp); // Added in JSAPI 14, Obsolete since JSAPI 32

JSObject *
JS_NewObjectForConstructor(JSContext *cx, const jsval *vp); // Obsolete since JSAPI 14
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.
clasp const JSClass * The class of the object to create. Added in SpiderMonkey 17
vp jsval * Pointer to a constructor. This must be an object that has a prototype property.
args JS::CallArgs & Call argument to get a constructor as callee. Added in SpiderMonkey 38

Description

JS_NewObjectForConstructor creates a new object exactly as the given constructor would if invoked with new. This function does not run the code of the constructor function--it just creates the object.

If the constructor argument is not an object jsval, this function will assert (in debug builds). If the constructor does not have a prototype property, this function will return NULL and set an exception on cx.

The standard object creation API, JS_NewObject, takes explicit arguments for the class, prototype, and parent of the new object. With JS_NewObjectForConstructor, the prototype and parent are determined by the constructor, and class is still determined by the caller. The prototype is the prototype of the constructor if it is an object, otherwise NULL. The parent is the constructor's parent.

See Also