JavaScript typeof运算符是否返回大写字符串? [英] Does the JavaScript typeof operator ever return an upper case string?

查看:159
本文介绍了JavaScript typeof运算符是否返回大写字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到了以下代码:

I recently came across the following line of code:

var type = (typeof x).toLowerCase();

请注意,在上面的代码中, x 将只是一个字符串,一个数字或未定义。我对此提出质疑,指出规范(11.4.3)说明要返回的值通过 typeof 运算符,所有这些都已经是小写。

Note that in the above code, x will only ever be a string, a number, or undefined. I questioned this, pointing out that the specification (11.4.3) states the values to be returned by the typeof operator, and all of them are already lower case.

值得注意的是规范离开主机对象可以自由地返回任何他们喜欢的东西,所以在这种情况下 可以获得带有大写字母的字符串(我不知道这是否真的发生过,但是它是允许的)。但是,如前所述,在这种情况下, x 只是一个字符串,一个数字或未定义。

It's worth noting that the specification leaves host objects free to return pretty much whatever they like, so in that case it is possible to get a string with some upper case letters (I don't whether that actually ever happens, but it is allowed). However, as already stated, in this case x is only ever a string, a number or undefined.

My问题是, typeof 运算符的任何实现是否会返回除小写字符串以外的任何内容?

My question is, do any implementations of the typeof operator ever return anything other than a lower case string?

推荐答案

Spidermonkey似乎只返回这些:

Spidermonkey seems to return only these:

"undefined"
"object"
"function"
"string"
"number"
"boolean"
NULL

https://developer.mozilla.org/ en / SpiderMonkey / JSAPI_Reference / JS_GetTypeName

与V8相同:

default:
  // For any kind of object not handled above, the spec rule for
  // host objects gives that it is okay to return "object"
  return isolate->heap()->object_symbol();

http://code.google.com/p/v8/source/browse/branches/bleeding_edge/src/runtime.cc#5245

不知道MS,我猜他们也不使用自定义类型,但你永远不会知道它们。

No idea about MS, I guess they don't use custom typeof either, but you never know with them.


typeof返回有六个可能的值:number,string,boolean,object,function和undefined。

There are six possible values that typeof returns: "number," "string," "boolean," "object," "function," and "undefined."

http://msdn.microsoft.com/en-us/library/windows/apps/259s7zc1%28v=vs.94%29.aspx

这篇关于JavaScript typeof运算符是否返回大写字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆