JS_NewFunction 编辑

Create a new JavaScript function that is implemented in C/C++ as a JSNative.

Syntax

// Added in SpiderMonkey 45

JSFunction *
JS_NewFunction(JSContext *cx, JSNative call, unsigned nargs, unsigned flags,
               const char *name);

// Obsolete since JSAPI 44

JSFunction *
JS_NewFunctionById(JSContext *cx, JSNative call, unsigned nargs, unsigned flags,
                   JS::Handle<jsid> id);

// Obsolete since JSAPI 39

JSFunction *
JS_NewFunction(JSContext *cx, JSNative call, unsigned nargs, unsigned flags,
               JS::Handle<JSObject*> parent, const char *name);

JSFunction *
JS_NewFunctionById(JSContext *cx, JSNative call, unsigned nargs, unsigned flags,
                   JS::Handle<JSObject*> parent, JS::Handle<jsid> id); // Added in SpiderMonkey 17
NameTypeDescription
cxJSContext *The context in which to create the new function. Requires request. In a JS_THREADSAFE build, the caller must be in a request on this JSContext.
callJSNativeNative C/C++ implementation of this function.
nargsunsignedNumber of arguments the function expects.
flagsunsignedPass JSFUN_CONSTRUCTOR to make a native that can be called as a constructor, otherwise 0.
parentJS::Handle&lt;JSObject*&gt;Pointer to the parent object for this function. If this is NULL, a default parent object is used. Obsolete since JSAPI 39
nameconst char *Name to assign to the new function. If this is NULL, the new function has no name.
idJS::Handle&lt;jsid&gt;A pointer to jsid of the name to assign to the new function. Added in SpiderMonkey 17

Description

JS_NewFunction creates a new JavaScript function implemented in C/C++. (To create a new function implemented in JavaScript, use JS_CompileFunction instead.)

call is a C/C++ function pointer that the new function wraps. nargs is the number of arguments the function expects. If flags is JSFUN_CONSTRUCTOR, a created function can be called as a constructor. parent may be used to specify the new function's parent; NULL is usually the right thing here. name is the name to assign to the function. If name is NULL, the new function has no name. (JS_GetFunctionId, passed the new function, will return NULL.)

On success, JS_NewFunction returns a pointer to the newly created function. Otherwise it reports an out-of-memory error and returns NULL.

See Also

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

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

发布评论

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

词条统计

浏览:96 次

字数:7619

最后编辑:7年前

编辑次数:0 次

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