JSFunction is a type in the JSAPI. The APIs JS_NewFunction, JS_DefineFunction, JS_CompileFunction, and their Unicode equivalents return values of type JSFunction *.

For native functions and JSAPI-compiled functions—that is, functions returned by the APIs listed above—there is a simple one-to-one relationship between the JSFunction and the corresponding JavaScript Function object. To get a JSFunction * given the JSObject * of a Function object, use JS_ValueToFunction. To get the JSObject * given the JSFunction *, use JS_GetFunctionObject.

For other function objects—that is, functions created by running JavaScript code containing function declarations or function-expressions—the relationship between the JSFunction * and the JSObject * is not well-defined. Different closures (Function objects) generated from the same source code may share the same JSFunction. As a result, some APIs (such as JS_CallFunction) that operate on JSFunctions do not work properly with closures. The documentation for each affected API contains a warning about the problem.

If the application can't be sure that a given function JSObject is either native or JSAPI-compiled, it must not use the affected APIs. Instead of using JS_CallFunction, for example, it must call JS_CallFunctionValue.

Function objects created by calling JS_NewFunction have two reserved slots for the application's use. See JS_NewFunction for details.

Document Tags and Contributors

 Last updated by: Jorend,