JS_DefineFunctions 编辑

Add native methods to an object.

Syntax

bool
JS_DefineFunctions(JSContext *cx, JS::Handle<JSObject*> obj,
                   const JSFunctionSpec *fs,
                   PropertyDefinitionBehavior behavior = DefineAllProperties);

In SpiderMonkey versions prior to SpiderMonkey 24, fs was not const.

NameTypeDescription
cxJSContext *The context in which to define functions. Requires request. In a JS_THREADSAFE build, the caller must be in a request on this JSContext.
objJS::Handle&lt;JSObject*&gt;The object on which functions are to be defined.
fsconst JSFunctionSpec *A NULL-terminated array of function specifications. Each element of the array defines an individual function.
behaviorPropertyDefinitionBehaviorSee below. Added in SpiderMonkey 38
enum PropertyDefinitionBehavior {
    DefineAllProperties,
    OnlyDefineLateProperties,
    DontDefineLateProperties
};
NameDescription
DefineAllPropertiesDefine all properties regardless of their flags.
OnlyDefineLatePropertiesDefine only properties which have a JSPROP_DEFINE_LATE flag.
DontDefineLatePropertiesDefine only properties which have no JSPROP_DEFINE_LATE flag.

Description

JS_DefineFunctions creates zero or more functions and makes them properties (methods) of a specified object, obj, as if by calling JS_DefineFunction repeatedly.

fs is a pointer to the first element of an array of JSFunctionSpec records. This array is usually defined as a static global, with each record initialized using JS_FS or JS_FN. Each array element defines a single function: its name, the native C/C++ implementation, the number of JavaScript arguments the function expects, and any property attributes. The last element of the array must contain 0 values. (The macro JS_FS_END can be used for the last element.) JS_DefineFunctions creates one function for each non-zero element in the array.

PropertyDefinitionBehavior is used to select if properties with JSPROP_DEFINE_LATE flag should be defined on the object. Normal JSAPI consumers probably always want DefineAllProperties here.

On success, JS_DefineFunctions returns true. On error or exception, it stops defining functions and returns false.

See Also

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

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

发布评论

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

词条统计

浏览:76 次

字数:6702

最后编辑:6年前

编辑次数:0 次

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