![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||||
| |||||
|
|
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. 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 |
|
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? |
#3
| |||
| |||
|
|
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. |
#4
| ||||
| ||||
|
|
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, |
|
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! |
#5
| |||
| |||
|
|
... unless the new object is referenced somewhere else on SubClass' [[prototype]] chain. |
#6
| |||
| |||
|
|
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. |
#7
| |||
| |||
|
|
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? |
Just like in Java, each object instance is
#8
| |||
| |||
|
|
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. |
![]() |
| Thread Tools | |
| Display Modes | |
| |