Retrieves the object for a specified function.

Syntax

JSObject *
JS_GetFunctionObject(JSFunction *fun);
Name Type Description
fun JSFunction * Pointer to a JS function. fun should be a native function or JSAPI-compiled function. See below.

Description

JS_GetFunctionObject returns the JavaScript Function object for a specified function pointer, fun.

fun must be either a function implemented by a JSNative (or JSFastNative) or the result of a call to JS_CompileFunction or similar functions. In other cases, the object returned by JS_GetFunctionObject is not necessarily safe to call and should not be exposed to script.

In the JavaScript language, functions are objects. But in the JSAPI there are two separate concepts: a JSObject is what is exposed to scripts and has properties and can be stored in variables; and the corresponding JSFunction contains the code of a function. For certain functions, the two have a strictly one-to-one relationship, and for those functions it is safe to call JS_GetFunctionObject to move from the JSFunction to the JSObject.

However, for other functions, and particularly for JavaScript closures, many JSObjects may share the same JSFunction. In these cases, this function should not be called. Calling the JSObject returned by it could cause a crash or worse.

JS_GetFunctionObject always returns a pointer to an object.

See Also