JS_DefineFunction 编辑

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

Syntax

JSFunction *
JS_DefineFunction(JSContext *cx, JS::Handle<JSObject*> obj,
                  const char *name, JSNative call,
                  unsigned nargs, unsigned attrs);

JSFunction *
JS_DefineUCFunction(JSContext *cx, JS::Handle<JSObject*> obj,
                    const char16_t *name, size_t namelen, JSNative call,
                    unsigned nargs, unsigned attrs);

JSFunction *
JS_DefineFunctionById(JSContext *cx, JS::Handle<JSObject*> obj,
                      JS::Handle<jsid> id, JSNative call,
                      unsigned nargs, unsigned attrs); // Added in SpiderMonkey 17
NameTypeDescription
cxJSContext *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.
objJS::Handle&lt;JSObject*&gt;Object for which to define a function as a property (method).
nameconst char * or const char16_t *Name to assign to the function.
idJS::Handle&lt;jsid&gt;A pointer to jsid for name to assign to the function. Added in SpiderMonkey 17
callJSNativeThe native C/C++ function to be wrapped by the new function.
nargsunsignedNumber of arguments that are passed to the function when it is called.
flagsunsignedThe logical OR of zero or more property attributes.

Description

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_DefineProperty. JS_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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:138 次

字数:8508

最后编辑:6年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文