A JSClass describes a class of JavaScript objects. A C/C++ program can use a JSClass with the JS_InitClass and JS_NewObject APIs to create objects that have custom methods and properties implemented in C/C++.
Syntax
struct JSClass {
    const char          *name;
    uint32_t            flags;
    /* Optional since SpiderMonkey 37 */
    JSPropertyOp        addProperty;
    JSDeletePropertyOp  delProperty;
    JSPropertyOp        getProperty;
    JSStrictPropertyOp  setProperty;
    JSEnumerateOp       enumerate;
    JSResolveOp         resolve;
    JSConvertOp         convert;      /* Obsolete since SpiderMonkey 44 */
    /* Optional since SpiderMonkey 25 */
    JSFinalizeOp        finalize;
    /* Optional */
    JSClassInternal     reserved0;    /* Obsolete since SpiderMonkey 13 */
    JSCheckAccessOp     checkAccess;  /* Obsolete since SpiderMonkey 29 */
    JSNative            call;
    JSHasInstanceOp     hasInstance;
    JSNative            construct;
    JSXDRObjectOp       xdrObject;    /* Obsolete since SpiderMonkey 13 */
    JSTraceOp           trace;        /* Added in SpiderMonkey 17 */
    JSClassInternal     reserved1;    /* Obsolete since SpiderMonkey 13 */
    void                *reserved[N]; /* sizeof 'reserved' depends on version */
};
| Name | Type | Description | 
|---|---|---|
| name | const char * | Class name | 
| flags | uint32_t | Class flags. This field is the bitwise OR of one or more of the JSCLASS_*constants described inJSClass.flags. | 
| addProperty | JSPropertyOp | A hook called just after adding a new property. May modify the new property value. Use  | 
| delProperty | JSDeletePropertyOp | A hook called when deleting a property. May veto. Use  | 
| getProperty | JSPropertyOp | A hook called when getting a property. This is the default getter for the class. Use  | 
| setProperty | JSStrictPropertyOp | A hook called when setting a property. This is the default setter for the class. When a script creates a new property, this is called after  Use  | 
| enumerate | JSEnumerateOp | Method for enumerating object properties. Use  Obsolete since JSAPI 37 The  | 
| resolve | JSResolveOp | Hook for implementing lazy properties. See  Use  Obsolete since JSAPI 36 The  Obsolete since JSAPI 16The  | 
| convert | JSConvertOp | This hook specifies how objects of this class are converted to a primitive value.  It implements the object's  Use  | 
| finalize | JSFinalizeOp | The object finalizer hook. This is called at some point after the object becomes unreachable, and can be used to free any additional resources associated with the object. If objects of this class do not need finalization, use  | 
| checkAccess | JSCheckAccessOp | Optional method for access checks. Implements JS_CheckAccess. If your application does not use the JSAPI's security features, useNULL. Obsolete since JSAPI 29 | 
| call | JSNative | This is called when a script calls an object as though it were a function: obj(). If objects of your class shouldn't be callable, useNULL. Most objects are not callable. | 
| hasInstance | JSHasInstanceOp | Pointer to an optional hasInstancemethod for this object. If you do not provide a method forhasInstance, set this pointer toNULL. | 
| construct | JSNative | This is called when a script uses an object as though it were a constructor: new obj().  If objects of your class shouldn't be callable this way, useNULL. Most custom objects are not intended to be used as constructors. | 
| xdrObject | JSXDRObjectOp | Implements object serialization. If you do not use JSXDR, set this pointer to NULL. Obsolete since JSAPI 13 | 
| trace | JSTraceOp | The GC trace hook. | 
Description
Use JSClass to define a custom class of JavaScript objects. A JSClass has a name, flags, and several callback functions for advanced customization of object behavior.
Simple classes
A simple custom class does not require any special JSClass callbacks. Instead, all the native code is in JSPropertyOps (property getters and setters) and JSNatives or JSFastNatives (methods). The code for such a class is simple:
/* SpiderMonkey 38 */
static JSClass robot_class = {
    "Robot",  /* name */
    0         /* flags */
};
/* SpiderMonkey 31 or older */
static JSClass robot_class = {
    "Robot",  /* name */
    0,        /* flags */
    JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
    JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, NULL,
    JSCLASS_NO_OPTIONAL_MEMBERS
};
To expose this class to scripts, and to attach methods and properties to it, use JS_InitClass.
Private data
Custom classes very often need to store private C/C++ data that should not be visible to scripts. To do this, use the JSCLASS_HAS_PRIVATE (JSCLASS_CONSTRUCT_PROTOTYPE wad removed) flags. Implement a JSNative constructor function that allocates this private data and stores it in the new object using JS_SetPrivate. Implement a JSClass.finalize callback that frees the private data. Each native getter, setter, or method can access the private data using JS_GetInstancePrivate.
When initializing the class, pass the JSNative constructor function to JS_InitClass.
(This example uses the C++ new and delete keywords, but the application can allocate the memory for private data however it likes. The JavaScript engine never touches the private data; it is for the application's use only.)
void
printer_finalize(JSContext *cx, JSObject *obj)
{
    MyPrinter *p = (MyPrinter *) JS_GetPrivate(cx, obj);
    delete p;
}
static JSClass printer_class = {
    "Printer",
    JSCLASS_HAS_PRIVATE,
    NULL, NULL, NULL, NULL,
    NULL, NULL, NULL, printer_finalize
};
/* SpiderMonkey 31 or older
 * static JSClass printer_class = {
 *     "Printer",
 *     JSCLASS_HAS_PRIVATE,
 *     JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
 *     JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, printer_finalize,
 *     JSCLASS_NO_OPTIONAL_MEMBERS
 * };
 */
bool
printer_construct(JSContext *cx, unsigned argc, jsval *vp)
{
    JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
    JSObject *obj = JS_NewObjectForConstructor(cx, &printer_class, args);
    /* SpiderMonkey 31 or older
     * JSObject *obj = JS_NewObjectForConstructor(cx, &printer_class, vp);
     */
    if (!obj)
        return false;
    MyPrinter *p = new MyPrinter;
    if (p == NULL) {
        JS_ReportOutOfMemory(cx);
        return false;
    }
    JS_SetPrivate(cx, obj, p);
    args.rval().setObject(*obj);
    /* SpiderMonkey 31 or older
     * JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(obj));
     */
    return true;
}
{
  JS_InitClass(cx, global, JS::NULL(), &printer_class, printer_construct, 1,
               NULL, NULL, NULL, NULL);
}
See also
- MXR ID Search for JSClass
- JSClass.flags
- JS_GetClass
- JS_InitClass
- JS_NewObject
- JS_NewObjectForConstructor
- JS_InstanceOf
- bug 638291 - added trace
- bug 702507 - removed JSCLASS_CONSTRUCT_PROTOTYPE
- bug 726944 - removed xdrObject,reserved0andreserved1
- bug 886829 - made finalizeoptional
- bug 957688 - removed checkAccess
- bug 1103368 - made most of members optional
- bug 1097267 - removed JSCLASS_NEW_ENUMERATE
- bug 1054756 - removed convert
- bug 1261723 - class ops are moved to a sub-structure JSClassOps