views:

63

answers:

2

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 operators 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.

+1  A: 

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.

Jon Skeet
The operand of `typeof` in JavaScript is any object or primitive, not specifically the name of a type. And no, JavaScript doesn't support operator overloading.
Tim Down
A: 

++ 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.

ormuriauga
() is a "function call" operator.
Šime Vidas
if you "call a function with arguments" or "apply a function to arguments" is just a matter of phrasing. but sure, given the `function.call()` method available in javascript, maybe your wording is preferred...
ormuriauga