JS_DefinePropertyWithTinyId 编辑

Obsolete since JSAPI 30
This feature is obsolete. Although it may still work in some browsers, its use is discouraged since it could be removed at any time. Try to avoid using it.

Create a new object property with a tiny id.

Syntax

JSBool
JS_DefinePropertyWithTinyId(
    JSContext *cx,
    JSObject *obj, const char *name, int8 tinyid,
    jsval value, JSPropertyOp getter, JSPropertyOp setter, unsigned int attrs);

JSBool
JS_DefineUCPropertyWithTinyId(
    JSContext *cx,
    JSObject *obj, const jschar *name, size_t namelen, int8 tinyid,
    jsval value, JSPropertyOp getter, JSPropertyOp setter, unsigned int attrs);
NameTypeDescription
cxJSContext *The context in which to define the property. Requires request. In a JS_THREADSAFE build, the caller must be in a request on this JSContext.
objJSObject *Object for which to create the new property.
nameconst char * or const jschar *Name for the property to create.
namelensize_t(only in JS_DefineUCPropertyWithTinyId) The length of name in characters; or (size_t) -1 to indicate that name is null-terminated.
tinyidint88-bit ID to aid in sharing getter and setter callbacks among properties.
valuejsvalInitial stored value for the new property.
getterJSPropertyOpgetProperty method for retrieving the current property value.
setterJSPropertyOpsetProperty method for specifying a new property value.
attrsunsigned intProperty attributes.

Description

JS_DefinePropertyWithTinyId defines an object property with a tiny id. JS_DefineUCPropertyWithTinyId is the Unicode version of the function. Except for the tinyid parameter, these functions behave exactly as JS_DefineProperty and JS_DefineUCProperty. See those functions for more details.

tinyid is a signed 8-bit integer that affects the id value passed to certain callbacks. Any time the JavaScript engine would pass the name of the property as a string to the id parameter of a tiny-id-aware callback, it passes INT_TO_JSVAL(tinyid) instead. The tiny-id-aware callbacks are: property getters and setters; and JSClass.addProperty, .delProperty, .getProperty, and .setProperty. Tiny ids only affect the ids passed to these hooks; the JavaScript engine does not use them as property names internally.

Tiny ids are helpful for getters and setters that are shared by several different properties. Those getters and setters can use switch (JSVAL_TO_INT(id)), instead of checking the value of id as a string, to determine which property is being accessed.

However, tiny ids can cause confusion when used with JSClass callbacks. For example, if a property named "color" is defined with tiny id 10, then the JavaScript expressions obj[10] and obj.color will both result in INT_TO_JSVAL(10) being passed to JSClass.getProperty as the id parameter. Per-property getters and setters do not suffer from this confusion: in the above example, obj.color would cause the JavaScript engine to call the getter for the color property, but obj[10] wouldn't. Traditionally, negative tiny ids are used to minimize the potential confusion, but it is best to specify a non-null getter and setter when defining a property with a tiny id.

On success, JS_DefinePropertyWithTinyId and JS_DefineUCPropertyWithTinyId return JS_TRUE. If the property already exists or cannot be created, they return JS_FALSE.

See Also

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

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

发布评论

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

词条统计

浏览:111 次

字数:8079

最后编辑:7年前

编辑次数:0 次

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