We're looking for a user researcher to understand the needs of developers and designers. Is this you or someone you know? Check out the post: https://mzl.la/2IGzdXS

JS DefineFunction

Create a native function and assign it as a property to a specified JS object.


JSFunction * JS_DefineFunction(JSContext *cx, JSObject *obj,
    const char *name, JSNative call, uintN nargs, uintN flags);

JSFunction * JS_DefineUCFunction(JSContext *cx, JSObject *obj,
    const jschar *name, size_t namelen, JSNative call,
    uintN nargs, uintN attrs);
Name Type Description
cx JSContext * The context in which to define the function. Requires request. In a JS_THREADSAFE build, the caller must be in a request on this JSContext.
obj JSObject * Object for which to define a function as a property (method).
name const char * or const jschar * Name to assign to the function.
call JSNative The native C/C++ function to be wrapped by the new function.
nargs uintN Number of arguments that are passed to the function when it is called.
flags uintN The logical OR of zero or more property attributes.


JS_DefineFunction exposes a C/C++ function to scripts by defining a new method on an existing JavaScript object. It creates the new Function object as though by calling JS_NewFunction, then makes it a method of obj as though by calling JS_DefinePropertyJS_DefineUCFunction is the Unicode version of the function.

name is the name of the property to create in obj. That is, it is the method name that scripts can use to call the new function. It also becomes the name property of the new function object and can be accessed using JS_GetFunctionId.

call is a pointer to the C/C++ function that is to be exposed to JavaScript.

nargs indicates the number of arguments the function expects to receive. This becomes the new function object's length property and can be accessed using JS_GetFunctionArity.

flags is the logical OR of zero or more property attributes, which apply to the new property.

On success, JS_DefineFunction and JS_DefineUCFunction return a pointer to the new function. On error or exception, they return NULL.

See Also

MXR ID Search for JS_DefineFunction
MXR ID Search for JS_DefineUCFunction

JS_CallFunctionName, JS_CallFunctionValue, JS_CompileFunction, JS_DefineFunctions, JS_DefineObject, JS_DefineProperties, JS_DefineProperty, JS_GetFunctionObject, JS_NewFunction

Document Tags and Contributors

Last updated by: Jorend,