JS_ConvertValue 编辑

Obsolete since JSAPI 38
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.

Converts a JavaScript value to a value of a specific JavaScript type.

Syntax

bool
JS_ConvertValue(JSContext *cx, JS::HandleValue v, JSType type, JS::MutableHandleValue vp);
NameTypeDescription
cxJSContext *The context in which to perform the conversion. Requires request. In a JS_THREADSAFE build, the caller must be in a request on this JSContext.
vJS::HandleValueThe value to convert.
typeJSTypeThe type to which the value is to be converted. type must be one of JSTYPE_VOID, JSTYPE_OBJECT, JSTYPE_FUNCTION, JSTYPE_STRING, JSTYPE_NUMBER, or JSTYPE_BOOLEAN. Otherwise JS_ConvertValue reports an error.
vpJS::MutableHandleValueOut parameter. On success, *vp receives the converted value.

Description

JS_ConvertValue converts a JavaScript value, v, to the specified type. On success, the converted value is stored in *vp. Typically users of this function set vp to point to v, so that if conversion is successful, v now contains the converted value.

JS_ConvertValue calls other, type-specific conversion routines based on the type argument.

  • Converting any value to JSTYPE_VOID always succeeds. The result is undefined value.
  • Converting to JSTYPE_OBJECT works exactly like JS::ToObject if v.isNullOrUndefined() is false, otherwise the result is nullptr.
  • Converting to JSTYPE_FUNCTION works like JS_ValueToFunction, but better: the result is a function object that has not been stripped of its lexical scope. It is safe to call the result (e.g. using JS_CallFunctionValue).
  • Converting to JSTYPE_STRING works like JS::ToString.
  • Converting to JSTYPE_NUMBER works exactly like JS::ToNumber.
  • Converting to JSTYPE_BOOLEAN works exactly like JS::ToBoolean.

On success, JS_ConvertValue stores the converted value in *vp and returns true. On error or exception, it returns false, and the value left in *vp is undefined.

See Also

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

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

发布评论

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

词条统计

浏览:66 次

字数:5039

最后编辑:7年前

编辑次数:0 次

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