instanceof

A

André Hänsel

Hi,

is instanceof an operator or a method of the object class?

On the web I found both ways. Actually I found a third way of
determining the class of an object: obj.constructor

So which is the correct one?

Regards,
André
 
R

RobG

Hi,

is instanceof an operator or a method of the object class?

Checking ECMA-262 might help:

11.8.6 The instanceof operator

On the web I found both ways. Actually I found a third way of
determining the class of an object: obj.constructor

instanceof of doesn't reliably tell you the [[class]] of an object,
nor does the constructor property. Search the archives for "Miller
Device"
 
L

Logos

RobG said:
instanceof of doesn't reliably tell you the [[class]] of an object,
nor does the constructor property. Search the archives for "Miller
Device"

I always just add a 'className' property to every class I create -
makes life mucho easy.
 
T

Thomas 'PointedEars' Lahn

André Hänsel said:
is instanceof an operator or a method of the object class?

The former.
On the web I found both ways. Actually I found a third way of
determining the class of an object: obj.constructor

There are no classes.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Logos said:
RobG said:
instanceof of doesn't reliably tell you the [[class]] of an object,
nor does the constructor property. Search the archives for "Miller
Device"

I always just add a 'className' property to every class I create -
makes life mucho easy.

There are still no classes. However, `className' is a used property name
and might result in confusion when reading the code (one might assume,
without further inspection, that the object in question is a HTML DOM object
and not a native object, because it has the `className' property).


PointedEars
 
L

Lasse Reichstein Nielsen

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
 
L

Logos

Thomas said:
Logos said:
RobG said:
instanceof of doesn't reliably tell you the [[class]] of an object,
nor does the constructor property. Search the archives for "Miller
Device"

I always just add a 'className' property to every class I create -
makes life mucho easy.

There are still no classes.

Nonetheless 99% of people are still gonna call them that. Objects can
be used in a very similar role, and it's an extremely handy label that
crosses language barriers - most people are going to understand what
you mean without long winded explanations.

However, `className' is a used property name
and might result in confusion when reading the code (one might assume,
without further inspection, that the object in question is a HTML DOM object
and not a native object, because it has the `className' property).

Certainly possible. However, I use a version of Hungarian notation in
my own coding which helps avoid those kind of errors (and please let
us not spin this off into why Hungarian notation is the antiChrist,
tho - some of us find it more helpful than harmful, some don't, big
deal).
 
A

André Hänsel

Nonetheless 99% of people are still gonna call them that.  Objects can
be used in a very similar role, and it's an extremely handy label that
crosses language barriers - most people are going to understand what
you mean without long winded explanations.

But this view leads to the confusion I (the OP) fell victim to. If I
focused my mind and remembered that JS is a prototyped language, my
question would have been unnecessary.
 
R

RobG

Thomas said:
Logos said:
RobG wrote:
instanceof of doesn't reliably tell you the [[class]] of an object,
nor does the constructor property. Search the archives for "Miller
Device"
I always just add a 'className' property to every class I create -
makes life mucho easy.
There are still no classes.

Nonetheless 99% of people are still gonna call them that.

Perhaps in your experience elsewhere, certainly not in this news
group.

Objects can
be used in a very similar role, and it's an extremely handy label that
crosses language barriers - most people are going to understand what
you mean without long winded explanations.

You might think it handy if talking to someone who doesn't know
anything about javascript. Hopefully those who might respond to posts
here are a little more knowledgeable and will appreciate the use of
correct terminology.

However, `className' is a used property name

class is an ECMA-262 future reserved word.
 
T

Thomas 'PointedEars' Lahn

[Fixed borken quoting]
class is an ECMA-262 future reserved word.

That is *also* a fact that should be considered when choosing identifiers.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Logos said:
Thomas said:
Logos said:
RobG wrote:
instanceof of doesn't reliably tell you the [[class]] of an object,
nor does the constructor property. Search the archives for "Miller
Device"
I always just add a 'className' property to every class I create -
makes life mucho easy.
There are still no classes.

Nonetheless 99% of people are still gonna call them that.

Even if that were true, one could also say 99% people call the Web "the
Internet"; they are wrong anyway.
Objects can be used in a very similar role,

No, they can't.
and it's an extremely handy label that
crosses language barriers - most people are going to understand what
you mean without long winded explanations.

IMNSHO most people, including you, don't know what they are talking
about when talking about ECMAScript implementations like JavaScript.

<http://javascript.crockford.com/javascript.html>


PointedEars
 
P

Peter Michaux

Logos said:
RobG said:
instanceof of doesn't reliably tell you the [[class]] of an object,
nor does the constructor property.  Search the archives for "Miller
Device"
I always just add a 'className' property to every class I create -
makes life mucho easy.

There are still no classes.

No one here has defined "class" but you say there aren't any. What is
a "class"?

Peter
 
T

Thomas 'PointedEars' Lahn

Peter said:
Thomas said:
Logos said:
RobG wrote:
instanceof of doesn't reliably tell you the [[class]] of an object,
nor does the constructor property. Search the archives for "Miller
Device"
I always just add a 'className' property to every class I create -
makes life mucho easy.
There are still no classes.

No one here has defined "class" but you say there aren't any. What is
a "class"?

Don't play stupid.


PointedEars
 
P

Peter Michaux

Peter said:
Thomas said:
Logos wrote:
RobG wrote:
instanceof of doesn't reliably tell you the [[class]] of an object,
nor does the constructor property.  Search the archives for "Miller
Device"
I always just add a 'className' property to every class I create -
makes life mucho easy.
There are still no classes.
No one here has defined "class" but you say there aren't any. What is
a "class"?

Don't play stupid.

It is a well known problem in the OOP world that "class" is not well
defined. I'm curious what you think a class is as you are stating
their aren't any in JavaScript.

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,100
Messages
2,570,635
Members
47,241
Latest member
HumbertoSt

Latest Threads

Top