André Hänsel said:
is instanceof an operator or a method of the object class?
It's an operator.
On the web I found both ways.
That's probably someone who has extended the Object or Function
prototype.
Actually I found a third way of determining the class of an object:
obj.constructor
Since there are no real classes in Javascript, that's not an absolutely
safe way to check whether something currently inherits from a specific
(constructor) function's prototype.
So which is the correct one?
For what?
insanceof checks whether its first operand (an object) has the prototype
property of its second operand (a function) in its inheritance chain.
obj.constructor checks whether the constructor property of an object
(which is typically inherited from its direct prototype) is equal
to a specific function.
Both work in the simple case where an object is created using the "new"
operator from a specific constructor function.
Either or both might break if:
1) The object's .constructor property is changed.
2) The function's .prototype property is changed.
3) The object's prototype is changed (using __proto__).
With "break" I mean that the object no longer tests as an instance of
the constructor function. However, that might really be the right
behavior in case 2 or 3.
I would generally trust the "constructor" property the least.
/L