Creates a new JavaScript object.


// Added in SpiderMonkey 45
JSObject *
JS_NewObject(JSContext *cx, const JSClass *clasp);

JS_NewObjectWithGivenProto(JSContext *cx, const JSClass *clasp, JS::Handle<JSObject*> proto);

// Obsolete since SpiderMonkey 38
JSObject *
JS_NewObject(JSContext *cx, const JSClass *clasp, JS::Handle<JSObject*> proto,
             JS::Handle<JSObject*> parent);

JSObject *
JS_NewObjectWithGivenProto(JSContext *cx, const JSClass *clasp, JS::Handle<JSObject*> proto,
                           JS::Handle<JSObject*> parent); // Added in SpiderMonkey 1.8
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 * Pointer to the class to use for the new object. If this is NULL, an ordinary JavaScript Object is created.

The JSClass may be used to override low-level object behavior, including such details as the physical memory layout of the object and how property lookups are done.

Added in SpiderMonkey 1.8.1 clasp->flags must not have the JSCLASS_GLOBAL_FLAGS bits set (use JS_NewGlobalObject instead).

If clasp is non-null, the caller must ensure that the JSClass remains alive throughout the lifetime of the new object, including the garbage collection cycle that finally frees it. The usual way to do this is to make JSClasses global or static.
proto JS::Handle&lt;JSObject*&gt;

Pointer to the prototype object to use for the new object. The new object will inherit all of the prototype object's properties and methods, and the new object's __proto__ property will be a reference to the prototype object.

JS_NewObject: If this is NULL, a default prototype object is used. Obsolete since JSAPI 38

JS_NewObject now always uses a default prototype object.

JS_NewObjectWithGivenProto: If this is NULL, the new object has no prototype; its __proto__ property is undefined.

parent JS::Handle&lt;JSObject*&gt; Pointer to the parent of the new object. The new object's __parent__ property will be a reference to this object. If parent is NULL, a default parent object is used. Obsolete since JSAPI 39


JS_NewObject creates a new object based on a specified class. cx is a pointer to a context associated with the runtime in which to establish the new object. clasp is a pointer to an existing class to use for internal methods, such as finalize. The JavaScript engine selects a prototype object for you.

On success, JS_NewObject returns a pointer to the new object. Otherwise it returns NULL.

Added in SpiderMonkey 1.8 JS_NewObjectWithGivenProto creates a new object with the specified prototype. If NULL is passed as proto, the new object will have a prototype with the JS value of null.

To create a new object as a property of an existing object, use JS_DefineObject, a convenience function that combines JS_NewObject and JS_DefineProperty.

Obsolete ?

Starting with Gecko 8.0 (Firefox 8.0 / Thunderbird 8.0 / SeaMonkey 2.5), you can create a new object in a specific compartment using the Components.utils.createObjectIn() method.

Choosing a default prototype

Like many JSAPI functions, JS_NewObject selects an appropriate prototype for the newly created object if you don't supply one yourself.  If the new object's class (here, clasp) is a built-in class, then its associated prototype is used as the new object's prototype. Otherwise, Object.prototype is used as the new object's prototype.

Previous behaviour Obsolete since JSAPI 45

Here's how the process works in detail: First, we must identify a global object.  If the parent is non-NULL, we assume it is part of a scope chain, walk to the end of that chain, and use the global object we find there.  If the parent is NULL, we use the global object at the end of the scope chain in which the context is currently executing. In other words, the context's current scope acts as a default parent.  If the context is not currently executing any code, we use JS_GetGlobalObject to find a global object associated with the context.

Next, we must find the given class's constructor in that global object. Normally we simply use the class's name as the name of the property of the global object to fetch. However, although JavaScript code can freely redefine constructors, the ECMAScript standard requires us in certain cases to use the original constructors' prototypes.  If the global object's class's flags include JSCLASS_GLOBAL_FLAGS, then the global object always retains the original constructors for each standard class; we use these original constructors when they are available.

Finally, we must find a prototype.  If the constructor has a prototype property, we use its value.  Otherwise, we use Object.prototype, looking up the constructor for Object as described above.

See Also