Visit Mozilla.org

JS DefineFunction

From MDC

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

[edit] Syntax

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 Indicates the built-in, native C call wrapped by this function.
nargs uintN Number of arguments that are passed to the function when it is called.
flags uintN Function attributes.

[edit] Description

JS_DefineFunction defines a single function and assigns it as a property (method) to a specified object, obj. JS_DefineUCFunction is the Unicode version of the function.

name is the name to assign to the function in the object. call is a built-in, native C call that is wrapped by your function. nargs indicates the number of arguments the function expects to receive. JS uses this information to allocate storage space for each argument.

flags lists the attributes to apply to the function. Currently documented attributes, JSFUN_BOUND_METHOD and JSFUN_GLOBAL_PARENT, are deprecated and should no longer be used. They continue to be supported only for existing applications that already depend on them.

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

[edit] See Also

LXR ID Search for JS_DefineFunction
LXR ID Search for JS_DefineUCFunction

JSFUN_BOUND_METHOD, JSFUN_GLOBAL_PARENT, JS_CallFunction, JS_CallFunctionName, JS_CallFunctionValue, JS_CompileFunction, JS_DecompileFunction, JS_DecompileFunctionBody, JS_DefineConstDoubles, JS_DefineElement, JS_DefineFunctions, JS_DefineObject, JS_DefineProperties, JS_DefineProperty, JS_DefinePropertyWithTinyId, JS_GetFunctionObject, JS_NewFunction, JS_SetBranchCallback, JS_ValueToFunction