Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Another perplexing instanceof question -- can anyone please explain?

Reply
Thread Tools

Another perplexing instanceof question -- can anyone please explain?

 
 
System Administrator
Guest
Posts: n/a
 
      03-10-2007
function a() { }

typeof a //returns 'function'
a instanceof a //returns false

typeof Object //returns 'function'
Object instanceof Object //returns true

Can anyone explain why when BOTH 'a' and 'Object'
are functions,
a instanceof a is false
BUT
Object instanceof Object is true ?

Best regards,
--j
 
Reply With Quote
 
 
 
 
Richard Cornford
Guest
Posts: n/a
 
      03-11-2007
David Golightly wrote:
> On Mar 10, 2:05 pm, System Administrator wrote:
>> function a() { }
>>
>> typeof a //returns 'function'
>> a instanceof a //returns false
>>
>> typeof Object //returns 'function'
>> Object instanceof Object //returns true
>>
>> Can anyone explain why when BOTH 'a' and 'Object'
>> are functions,
>> a instanceof a is false
>> BUT
>> Object instanceof Object is true ?

>
> It's because all objects are instances of Object,
> including itself.


Technically it is because the - instanceof - operator returns a value
based upon whether its left hand operator has the object on its prototype
chain that is the object currently referred to by the - prototype -
property of the function object that is its right hand side operator
(exceptions are thrown if the left hand operand is not an object or the
right hand operand does no have a [[HasInstance]] method (so is not a
function)). The object referred to by the - prototype - property of -
Object - is on (at the end of) the prototype chain of all objects except
for its own (as it is the only object in javascript that can have a null
[[Prototype]] property.

However, because - instanceof - is dynamically evaluated at runtime and
the - prototype - properties of all non build-in functions can be
assigned at runtime it is not necessary that - (objectConstructedWithA
instance of A) - always be true.

> Non-Objects include:
>
> null
> undefined
> NaN
> string primitives ('foo', 'bar')
> number primitives (1, 2, 3)


Either NaN is a number primitive (along with Infinity) or undefined,
infinity and NaN are all global variables which may have their values
re-assigned at runtime but start off with the Undefined primitive value,
the positive infinity numeric primitive value and the NaN numeric
primitive values, respectively.

> boolean primitives (true, false)
>
> However, "a" is not an instanceof itself, it *is* itself. So:


The object referred to by the - prototype - property of - a - is not on
the prototype chain of a.

> var b = new a;
> b instanceof a; >>> true
> a === a; >>> true
> b === a; >>> false
>
> Also, in JS, "Object" serves not only as an object, but also
> as a generic object constructor, therefore a function. Also,
> all functions are objects. Try:
>
> var k = {}; // object literal
> k instanceof Object; >>> true
> k instanceof Function; >>> false, because k was not created via
> constructor and therefore has no prototype property

<snip>

Objects created with a constructor (with the exception of objects with
function objects on their prototype chain) do not have - prototype -
properties. So - k - not being created with a constructor has no
significance.

Richard.

 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Perplexing problem Anyone any good idea ? Channel 53 Wireless Networking 7 01-31-2009 12:23 PM
I have a very perplexing question iL_weReo Computer Support 3 12-05-2007 06:51 AM
instanceof NOT (always) bad? The instanceof myth. dmx_dawg@hotmail.com Java 21 07-20-2006 07:06 PM
this weeks perplexing question billyw MCSE 11 09-21-2004 03:22 PM
Perplexing problem with inserting into a linkedlist object Anonymous Sender Java 7 05-25-2004 10:27 AM



Advertisments