napi_typeof
napi_status napi_typeof(napi_env env, napi_value value, napi_valuetype* result) [in] env:调用该 API 时所处的环境。[in] value:要查询其类型的 JavaScript 值。[out] result:JavaScript 值的类型。
如果 API 成功,则返回 napi_ok。
【Returns napi_ok if the API succeeded.】
- 如果
value的类型不是已知的 ECMAScript 类型,并且value不是 External 值,则返回napi_invalid_arg。
该 API 表现出类似于在对象上调用 typeof 运算符的行为,如 ECMAScript 语言规范 typeof 运算符 中定义的那样。然而,也存在一些差异:
【This API represents behavior similar to invoking the typeof Operator on
the object as defined in Section typeof operator of the ECMAScript Language
Specification. However, there are some differences:】
- 它支持检测外部值。
- 它将
null检测为一种独立类型,而 ECMAScript 的typeof会将其检测为object。
如果 value 的类型无效,将返回错误。
【If value has a type that is invalid, an error is returned.】