typeof是一个操作符和一个函数 [英] typeof is an operator and a function
问题描述
在JavaScript中 typeof
是一个运算符和一个函数。它更适合用作操作员还是功能?为什么?
In JavaScript typeof
is an operator and a function. Is it better used as an operator or a function? Why?
推荐答案
typeof
是一个运算符。您可以使用以下方式轻松查看:
typeof
is an operator. You can easily check it using:
typeof(typeof)
是 typeof
一个函数,这个表达式将返回'function'
string,但会导致语法错误:
Were typeof
a function, this expression would return 'function'
string, but it results in a syntax error:
js> typeof(typeof);
typein:8: SyntaxError: syntax error:
typein:8: typeof(typeof);
typein:8: .............^
所以, typeof
不能是一个函数。可能是括号 - 符号 typeof(foo)
让你认为 typeof
是一个函数,但从语法上讲,这些括号不是函数调用 - 它们是用于分组的,就像(2 + 3)* 2
。事实上,你可以添加任意数量的它们:
so, typeof
cannot be a function. Probably parenthesis-notation typeof(foo)
made you think typeof
is a function, but syntactically, those parenthesis are not function call - they are those used for grouping, just like (2 + 3) *2
. In fact, you can add any number of them you want:
typeof(((((foo))))); // is equal to typeof foo;
这篇关于typeof是一个操作符和一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!