HighDots Forums  

How instanceof works?

Javascript JavaScript language (comp.lang.javascript)


Discuss How instanceof works? in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Dolaameng
 
Posts: n/a

Default How instanceof works? - 11-02-2009 , 01:11 AM






Hi, guys, I am a newbie to javascript. I'd like to find out how the
'instanceof' operator works in javascript? What kind of tests are
going on behind the scene? For example, in following code, I
constructed a 'Base' class, and a 'Derived' class to extend it. Then I
instantiate an instance of 'Derived' to test if it is 'instanceof' the
Base class:

<script type="text/javascript">
//<![CDATA[
function extend(SubClass, SupClass){
var F=function(){};
F.prototype=SupClass.prototype;
SubClass.prototype=new F();
SubClass.prototype.constructor=SubClass;
//...
}
var Base=function(){};
//Base.prototype={};
var Derived=function(){
//Base.apply(this,null);
};
extend(Derived,Base);
var d=new Derived();

alert(d instanceof Base); //true
//]]>
</script>

The instanceof test above evaluates to true, with no surprise. I
guess this is because the interpreter found that
'Derived.prototype.prototype.constructor' is now 'Base'. So I add a
new line of code

Base.prototype={}

between the definition of Base and Derived class. This time I know
that 'Base.prototype.constructor' is not 'Base' anymore, it has been
overwritten to 'Object' instead. But superisely 'd instanceof Base'
still evaluates to true. Why does this magic happen?

Reply With Quote
  #2  
Old   
Richard Cornford
 
Posts: n/a

Default Re: How instanceof works? - 11-02-2009 , 04:06 AM






Dolaameng wrote:
Quote:
Hi, guys, I am a newbie to javascript. I'd like to find out how
the 'instanceof' operator works in javascript?
The standard for javascript is ECMA 262, currently 3rd edition, which
defines the behaviour required of ECMAScript implementations (which is
what current versions of javascript claim to be). That document will ay
exactly what - instanceof - can be expected to do.

Quote:
What kind of tests are
going on behind the scene?
The instance of operator performs a runtime comparison between the
identity of the current value of a function's - prototype - property
and, in tern, each of the objects on an object's internal prototype
chain. If any of the objects on the prototype chain are the object that
the function's - prototype - property refers to then the result is true.
This isn't a hugely useful relationship to test so - instanceof - is not
that frequently used.

Quote:
For example, in following code, I
constructed a 'Base' class, and a 'Derived' class to extend it.
snip>> alert(d instanceof Base); //true
//]]
/script

The instanceof test above evaluates to true, with no surprise. I
guess this is because the interpreter found that
'Derived.prototype.prototype.constructor' is now 'Base'. So I
add a new line of code
The - constructor - property of object's is not involved in the
specified behaviour.

Quote:
Base.prototype={}

between the definition of Base and Derived class. This time I
know that 'Base.prototype.constructor' is not 'Base' anymore,
it has been overwritten to 'Object' instead. But superisely
'd instanceof Base' still evaluates to true.
No it doesn't.

Quote:
Why does this magic happen?
My guess is that you typoed - prototype - and ended up assigning the
object to the wrong property of - Base - (leaving its original -
prototype - property referring to its original value.

Richard.

Reply With Quote
  #3  
Old   
Dolaameng
 
Posts: n/a

Default Re: How instanceof works? - 11-02-2009 , 09:09 PM



On Nov 2, 5:06*pm, "Richard Cornford" <Rich... (AT) litotes (DOT) demon.co.uk>
wrote:
Quote:
Dolaameng wrote:
Hi, guys, I am a newbie to javascript. I'd like to find out how
the 'instanceof' operator works in javascript?

The standard for javascript is ECMA 262, currently 3rd edition, which
defines the behaviour required of ECMAScript implementations (which is
what current versions of javascript claim to be). That document will ay
exactly what - instanceof - can be expected to do.

What kind of tests are
going on behind the scene?

The instance of operator performs a runtime comparison between the
identity of the current value of a function's - prototype - property
and, in tern, each of the objects on an object's internal prototype
chain. If any of the objects on the prototype chain are the object that
the function's - prototype - property refers to then the result is true.
This isn't a hugely useful relationship to test so - instanceof - is not
that frequently used.

For example, in following code, I
constructed a 'Base' class, and a 'Derived' class to extend it.

snip>> alert(d instanceof Base); //true

//]]
/script

The instanceof test above evaluates to true, with no surprise. *I
guess this is because the interpreter found that
'Derived.prototype.prototype.constructor' is now *'Base'. *So I
add a new line of code

The - constructor - property of object's is not involved in the
specified behaviour.

Base.prototype={}

between the definition of Base and Derived class. This time I
know that 'Base.prototype.constructor' is not 'Base' anymore,
it has been overwritten to 'Object' instead. But superisely
'd instanceof Base' still evaluates to true.

No it doesn't.

Why does this magic happen?

My guess is that you typoed - prototype - and ended up assigning the
object to the wrong property of - Base - (leaving its original -
prototype - property referring to its original value.

Richard.
Thanks Richard for you useful explanation. However, when I insert the
line 'Base.prototype={};' (with no typo) BEFORE invoking 'extend
(Derived,Base);', the statement 'alert(d instanceof Base );' indeed
evaluates to true (in firefox 3.5.3).

But if I invoke 'Base.prototype={};' AFTER 'extend(Derived,Base);',
the instanceof operation now evaluates to false as you said.

So I realized this could be due to the implementation of the 'extend'
function in my code, where I made a copy of SupClass.prototype through
a temp function F. If I invoke 'Base.prototype={};' BEFORE calling
extend, that actually means I just add the an empty object into
SubClass's Chain, and now Base.prototype is also that same empty
object. As a result 'd instanceof Base' is true.
If I change the invocation order, that will cause a mismatch between
Base.prototype and the one added in SubClass's prototype chain, so the
instanceof operation evaluates to false.

Is what I said here correct? thanks again!

Reply With Quote
  #4  
Old   
RobG
 
Posts: n/a

Default Re: How instanceof works? - 11-02-2009 , 09:49 PM



On Nov 3, 12:09 pm, Dolaameng <ma.li... (AT) gmail (DOT) com> wrote:
Quote:
On Nov 2, 5:06 pm, "Richard Cornford" <Rich... (AT) litotes (DOT) demon.co.uk
wrote:



Dolaameng wrote:
Hi, guys, I am a newbie to javascript. I'd like to find out how
the 'instanceof' operator works in javascript?

The standard for javascript is ECMA 262, currently 3rd edition, which
defines the behaviour required of ECMAScript implementations (which is
what current versions of javascript claim to be). That document will ay
exactly what - instanceof - can be expected to do.

What kind of tests are
going on behind the scene?

The instance of operator performs a runtime comparison between the
identity of the current value of a function's - prototype - property
and, in tern, each of the objects on an object's internal prototype
chain. If any of the objects on the prototype chain are the object that
the function's - prototype - property refers to then the result is true.
This isn't a hugely useful relationship to test so - instanceof - is not
that frequently used.

For example, in following code, I
constructed a 'Base' class, and a 'Derived' class to extend it.

snip>> alert(d instanceof Base); //true

//]]
/script

The instanceof test above evaluates to true, with no surprise. I
guess this is because the interpreter found that
'Derived.prototype.prototype.constructor' is now 'Base'. So I
add a new line of code

The - constructor - property of object's is not involved in the
specified behaviour.

Base.prototype={}

between the definition of Base and Derived class. This time I
know that 'Base.prototype.constructor' is not 'Base' anymore,
it has been overwritten to 'Object' instead. But superisely
'd instanceof Base' still evaluates to true.

No it doesn't.

Why does this magic happen?

My guess is that you typoed - prototype - and ended up assigning the
object to the wrong property of - Base - (leaving its original -
prototype - property referring to its original value.

Thanks Richard for you useful explanation. However, when I insert the
line 'Base.prototype={};' (with no typo) BEFORE invoking 'extend
(Derived,Base);', the statement 'alert(d instanceof Base );' indeed
evaluates to true (in firefox 3.5.3).

But if I invoke 'Base.prototype={};' AFTER 'extend(Derived,Base);',
the instanceof operation now evaluates to false as you said.

So I realized this could be due to the implementation of the 'extend'
function in my code,
Not the implementation, but the order of assignment and execution of
surrounding code (see below).

Quote:
where I made a copy of SupClass.prototype through
a temp function F.
You don't make a copy, you create another reference to it. It is
impossible to make a copy of an object in ECMAScript such that given
objects ObjectA and ObjectB then:

ObjectA === ObjectB

evaluates to true if and only if ObjectA and ObjectB refer to the same
object (where object includes functions). You can copy the properties
and values of one object to another (which is what some extend
functions do).


Quote:
If I invoke 'Base.prototype={};' BEFORE calling
extend, that actually means I just add the an empty object into
Not add, you assign a reference to that empty object to
Base.prototype, replacing the previous reference.

Quote:
SubClass's Chain, and now Base.prototype is also that same empty
object. As a result 'd instanceof Base' is true.
If I change the invocation order, that will cause a mismatch between
Base.prototype and the one added in SubClass's prototype chain, so the
instanceof operation evaluates to false.

Is what I said here correct? thanks again!
Yes. The object referenced by SubClass[[prototype]] is still the
object referenced by Base.prototype SubClass was constructed. The
instanceof operator uses the current value of Base.prototype, so if
that now refers to some other object, the result will be false unless
the new object is referenced somewhere else on SubClass' [[prototype]]
chain.


--
Rob

Reply With Quote
  #5  
Old   
RobG
 
Posts: n/a

Default Re: How instanceof works? - 11-02-2009 , 09:56 PM



On Nov 3, 12:49*pm, RobG <rg... (AT) iinet (DOT) net.au> wrote:
Quote:
... unless
the new object is referenced somewhere else on SubClass' [[prototype]]
chain.
Ooops, somewhere else on d's [[prototype]] chain.


--
Rob

Reply With Quote
  #6  
Old   
Dolaameng
 
Posts: n/a

Default Re: How instanceof works? - 11-03-2009 , 08:34 PM



On Nov 3, 5:25*pm, "Richard Cornford" <Rich... (AT) litotes (DOT) demon.co.uk>
wrote:
Quote:
RobG wrote:
On Nov 2, 7:06 pm, Richard Cornford wrote:
snip
The instance of operator performs a runtime comparison between
the identity of the current value of a function's - prototype
- property and, in tern, each of the objects on an object's
internal prototype chain. If any of the objects on the prototype
chain are the object that the function's - prototype - property
refers to then the result is true. This isn't a hugely useful
relationship to test so - instanceof - is not that frequently
used.

If only you would use your talent for such clear and succinct
explanations (sans typos, of course) to write a book covering
all the parts of ECMAScript, I'm sure it would be very well
received. Why not 'ECMAScript: All the Parts'? Heaven forbid,
you might make money from it. *:-)

snip

The thing that I would question is not that money could be made but
whether that money would be a suitable return for the time and effort
involved. Particularly in comparison with javascript programming for a
living, baring in mind that I am currently employed as a specialist and
very well paid for that.

Any javascript book would be, at best, targeted at a niche market, and
so have relatively restricted sales potential. So, financially, it looks
like my best interests lie in carrying on doing what I am doing now.
Things may change, but it does not look very likely that they will in
the foreseeable future.

Richard.
Thanks guys, I do learn here. Would you recommend to me some good
books about how javascript works inside? For example, I like very
much the style of <inside c++ object model> and <effective c++>. They
show you how C++ works from inside out. So i am wondering if there are
any similiar books in javascript? As you know, deciphering the ECMA
standard is really tough for a beginner, and boring.... Sometime I am
just not confident when using some features of javascript in my code.
For example, I am usually not quite sure if 'instanceof' or
'constructor test' would work for me in the code, or does the 'this'
variable refer to the right object here? That is really frustrating.

Reply With Quote
  #7  
Old   
VK
 
Posts: n/a

Default Re: How instanceof works? - 11-04-2009 , 01:03 PM



Dolaameng wrote:
Quote:
So I add a
new line of code

Base.prototype={}

between the definition of Base and Derived class. This time I know
that 'Base.prototype.constructor' is not 'Base' anymore, it has been
overwritten to 'Object' instead. But superisely 'd instanceof Base'
still evaluates to true. Why does this magic happen?
I guess you didn't program Java from were instanceof "syntax sugar"
has been taken Just like in Java, each object instance is
instanceof of its own class and instanceof of generic Object. That has
a structural sense though might not be obvious.

function MyObject() {
this.foo = 'bar';
}

var obj = new MyObject;

window.alert(obj instanceof MyObject); // true

window.alert(obj instanceof Object); // true

I highly recommend Eric Lippert blog article "The JScript Type System,
Part Two: Prototypes and constructors" at
http://blogs.msdn.com/ericlippert/archive/2003/11/06/53352.aspx
That is the only source I know where some matters are described
decently.

Reply With Quote
  #8  
Old   
VK
 
Posts: n/a

Default Re: How instanceof works? - 11-04-2009 , 01:07 PM



Quote:
between the definition of Base and Derived class. This time I
know that 'Base.prototype.constructor' is not 'Base' anymore,
it has been overwritten to 'Object' instead. But superisely
'd instanceof Base' still evaluates to true.

No it doesn't.
Of course if does (see my answer). Do not read ECMA 262 3rd.ed. too
often, it makes a damage, I told you so many times.


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.