Add native methods to an object.


JS_DefineFunctions(JSContext *cx, JS::Handle<JSObject*> obj,
                   const JSFunctionSpec *fs,
                   PropertyDefinitionBehavior behavior = DefineAllProperties);

In SpiderMonkey versions prior to SpiderMonkey 24, fs was not const.

Name Type Description
cx JSContext * The context in which to define functions. Requires request. In a JS_THREADSAFE build, the caller must be in a request on this JSContext.
obj JS::Handle&lt;JSObject*&gt; The object on which functions are to be defined.
fs const JSFunctionSpec * A NULL-terminated array of function specifications. Each element of the array defines an individual function.
behavior PropertyDefinitionBehavior See below. Added in SpiderMonkey 38
enum PropertyDefinitionBehavior {
Name Description
DefineAllProperties Define all properties regardless of their flags.
OnlyDefineLateProperties Define only properties which have a JSPROP_DEFINE_LATE flag.
DontDefineLateProperties Define only properties which have no JSPROP_DEFINE_LATE flag.


JS_DefineFunctions creates zero or more functions and makes them properties (methods) of a specified object, obj, as if by calling JS_DefineFunction repeatedly.

fs is a pointer to the first element of an array of JSFunctionSpec records. This array is usually defined as a static global, with each record initialized using JS_FS or JS_FN. Each array element defines a single function: its name, the native C/C++ implementation, the number of JavaScript arguments the function expects, and any property attributes. The last element of the array must contain 0 values. (The macro JS_FS_END can be used for the last element.) JS_DefineFunctions creates one function for each non-zero element in the array.

PropertyDefinitionBehavior is used to select if properties with JSPROP_DEFINE_LATE flag should be defined on the object. Normal JSAPI consumers probably always want DefineAllProperties here.

On success, JS_DefineFunctions returns true. On error or exception, it stops defining functions and returns false.

See Also