We're looking for a user researcher to understand the needs of developers and designers. Is this you or someone you know? Check out the post: https://mzl.la/2IGzdXS

JS NewObject

Creates a new JavaScript object.


JSObject * JS_NewObject(JSContext *cx, JSClass *clasp, JSObject *proto,
    JSObject *parent);

JSObject * JS_NewObjectWithGivenProto(JSContext *cx, JSClass *clasp, JSObject *proto,
    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 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 JSObject *

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.

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

parent JSObject * 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.


JS_NewObject creates a new object based on a specified class, prototype, and parent object. 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. proto is an optional pointer to the prototype object with which to associate the new object.

If proto is NULL, the JavaScript engine selects a prototype object for you. In this case, JS_NewObject attempts to assign the new object the prototype object belonging to clasp, if one is defined there. Otherwise, it creates an empty object stub for the prototype.

parent is an optional pointer to an existing object to which to set the new object's parent object property. You can set parent to NULL if you do not want to set the __parent__ property. See JS_GetParent for details on what the parent property does.

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

Added in SpiderMonkey 1.8 JS_NewObjectWithGivenProto behaves exactly the same, except that if proto is NULL, it creates an object with no prototype.

If clasp has a constructor (JSClass.construct), use JS_ConstructObject instead to ensure that the constructor is called.

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.

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.createInObject() 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.  In each case, the choice is driven by the new object's class (here, clasp), its parent scope (here, parent), and perhaps the context (here, cx).  Roughly speaking, we use the parent or the context to find a global object, find the given class's constructor in that global object, and then use that constructor's prototype.

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

MXR ID Search for JS_NewObject
MXR ID Search for JS_NewObjectWithGivenProto

JS_NewGlobalObject, JS_NewArrayObject, JS_ValueToObject

Document Tags and Contributors

Contributors to this page: Jorend
Last updated by: Jorend,