JS DefineFunctions

Add native methods to an object.


JSBool JS_DefineFunctions(JSContext *cx, JSObject *obj, const JSFunctionSpec *fs);
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 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.
In SpiderMonkey versions prior to SpiderMonkey 24, the last argument to JS_DefineFunctions was not const.


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 function flags and 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.

On success, JS_DefineFunctions returns JS_TRUE. On error or exception, it stops defining functions and returns JS_FALSE.

See Also

MXR ID Search for JS_DefineFunctions

JS_CallFunctionNameJS_DefineObject, JS_DefineProperties, JS_NewFunction

Document Tags and Contributors

Last updated by: Jorend,