Add native methods to an object.
Syntax
bool
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_THREADSAFEbuild, the caller must be in a request on thisJSContext. | 
| obj | JS::Handle<JSObject*> | 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 {
    DefineAllProperties,
    OnlyDefineLateProperties,
    DontDefineLateProperties
};
| Name | Description | 
|---|---|
| DefineAllProperties | Define all properties regardless of their flags. | 
| OnlyDefineLateProperties | Define only properties which have a JSPROP_DEFINE_LATEflag. | 
| DontDefineLateProperties | Define only properties which have no JSPROP_DEFINE_LATEflag. | 
Description
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
- MXR ID Search for JS_DefineFunctions
- JS_CallFunctionName
- JS_DefineObject
- JS_DefineProperties
- JS_NewFunction
- bug 959787 - make fstype toconst
- bug 825199 - added PropertyDefinitionBehavior