开发者

Why typeof is called operator instead of function?

While reading this article (Special Operators) in MDC, I got a question like why typeof, new, etc. are called operators?

I have a conception say + is a operator because it oper开发者_如何转开发ators on two entities like 2+3 and gives another value.

Ofcourse functions also operates on say two entities like the same way.

Then what is difference?

Thanks for any answers.


No, being an operator isn't about whether or not it's got two operands - that's the difference between a unary operator and a binary operator (and then there's the conditional operator with three operands, which is an example of a ternary operator). For example, + can also be a unary operator:

var x = +5;

You can sort of thing of operators as "built-in functions". They're known about by the JavaScript engine itself, and are part of the language rather than just being library features. (Although operators can often be overloaded by libraries, depending on the language. I don't know if JavaScript supports operator overloading, offhand.)

In some cases (JavaScript being an example) there's a bit of a blurring between the language and the standard library, but think about what typeof takes as an operand: it's the name of a type. That's not something you can use normally as a function argument; it needs special support in the language.


++ is also an operator with only one argument.

One of the differences is that you don't call an operator with parameters, i.e you write typeof a or new SomeObject(), whereas a function type_of would be called with type_of(a). But like Jon said, you can kind of think of them as built-in functions. As a side note () in myFunc(arg1, arg2) is a function-application operator, i.e it invokes a function with the arguments given.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜