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
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 | JS::Handle<JSObject*> | Object for which to define a function as a property (method). |
name | const char * or const char16_t * | Name to assign to the function. |
id | JS::Handle<jsid> | A pointer to jsid for name to assign to the function. Added in SpiderMonkey 17 |
call | JSNative | The native C/C++ function to be wrapped by the new function. |
nargs | unsigned | Number of arguments that are passed to the function when it is called. |
flags | unsigned | The 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
- MXR ID Search for
JS_DefineFunction
- MXR ID Search for
JS_DefineUCFunction
- MXR ID Search for
JS_DefineFunctionById
JS_CallFunctionName
JS_CallFunctionValue
JS_CompileFunction
JS_DefineFunctions
JS_DefineObject
JS_DefineProperties
JS_DefineProperty
JS_GetFunctionObject
JS_NewFunction
- bug 607695 - added
JS_DefineFunctionById
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论