JavaScript-1.5 Reference

JS_InitClass Function

Summary

Initializes a class structure, its prototype, properties, and functions.

Syntax

JSObject * JS_InitClass(JSContext *cx, JSObject *obj,
    JSObject *parent_proto, JSClass *clasp,
    JSNative constructor, uintN nargs, JSPropertySpec *ps,
    JSFunctionSpec *fs, JSPropertySpec *static_ps,
    JSFunctionSpec *static_fs);
NameTypeDescription
cxJSContext *Pointer to a JS context from which to derive runtime information.
objJSObject *Pointer to the object to use for initializing the class.
parent_protoJSObject *Pointer to a prototype object for the class.
claspJSClass *Pointer to the class structure to initialize. This structure defines the class for use by other API functions.
constructorJSNativeThe constructor for the class. Its scope matches that of the obj argument. If constructor is NULL, then static_ps and static_fs are also NULL.
nargsuintNNumber of arguments for the constructor.
psJSPropertySpec *Pointer to the properties structure for the prototype object, parent_proto.
fsJSFunctionSpec *Pointer to the functions structure for the prototype object, parent_proto.
static_psJSPropertySpec *Pointer to the properties structure for the constructor object, if it is not NULL.
static_fsJSFunctionSpec *Pointer to the functions structure for the constructor object, if it is not NULL.

Description

JS_InitClass builds a class structure, its object constructor, its prototype, its properties, and its methods. A class is an internal JS structure that is not exposed outside the JS engine. You can use a class, its properties, methods, and prototypes to build other objects that are exposed outside the engine.

JS_InitClass returns a pointer to a JS object that is the prototype for the newly initialized class. If JS_InitClass fails, then the pointer returned is NULL.

A class is comprised of a class structure, a constructor, a prototype object, and properties and functions. The class structure specifies the name of the class, its flags, and its property functions. These include functions for adding and deleting properties, getting and setting property values, and enumerating converting, resolving, and finalizing its properties.

The constructor for the class is built in the same context as cx, and in the same scope as obj. If you pass NULL to JS_InitClass, then a constructor is not built, and you cannot specify static properties and functions for the class.

If you provide a constructor for the class, then you should also pass an object to parent_proto. JS_InitClass uses parent_proto to build a prototype accessor object for the class. The accessor object is modeled on the prototype object you provide. If the accessor object is successfully created, JS_InitClass returns a pointer to the JS object. Otherwise it returns NULL, indicating failure to create the accessor object, and therefore failure to create the class itself.

After building the constructor and prototype, JS_InitClass adds the properties and methods of the constructor and prototype, if any, to the class definition. Properties and methods are either "dynamic," based on the properties and methods of the prototype object, or "static," based on the properties and methods of the constructor.

See Also

Groups [ Function ]
Documents [ LXR ID Search ]
Entries [ JSClass | JSFunctionSpec | JSPropertySpec | JS_GetClass | JS_InstanceOf ]

This page was generated by APIDOC