JS_SetPrototype

Set a JavaScript object's prototype.

Syntax

bool
JS_SetPrototype(JSContext *cx, JS::HandleObject obj, JS::HandleObject proto);
Name Type Description
cx JSContext * The context in which to set the object's prototype. Requires request. In a JS_THREADSAFE build, the caller must be in a request on this JSContext.
obj JS::HandleObject The object to modify.
proto JS::HandleObject The object to set as the new prototype of obj.

Description

JS_SetPrototype sets the prototype object for a specified object. A prototype object provides properties that are shared by similar JS object instances. Ordinarily you set a prototype for an object when you create the object with JS_NewObject, but if you do not set a prototype at that time, you can later call JS_SetPrototype to do so.

obj is a pointer to an existing JS object, and proto is a pointer to second existing object upon which the first object is to be based.

On success, JS_SetPrototype returns true. Otherwise it returns false.

Take care not to create a circularly-linked list of prototypes using this function, because such a set of prototypes cannot be resolved by the JavaScript engine and can easily lead to an infinite loop.

To get an object's prototype, use JS_GetPrototype.

See Also