473,378 Members | 1,489 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Another perplexing instanceof question -- can anyone please explain?

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
Mar 10 '07 #1
2 1667
On Mar 10, 2:05 pm, System Administrator
<f.dhjkafjkl...@fjk.dsfjkds.comwrote:
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
It's because all objects are instances of Object, including itself.
Non-Objects include:

null
undefined
NaN
string primitives ('foo', 'bar')
number primitives (1, 2, 3)
boolean primitives (true, false)

However, "a" is not an instanceof itself, it *is* itself. So:

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

var j = function() {}; // function literal
j instanceof Object; >>true
j instanceof Function; >>true

-David

Mar 10 '07 #2
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.

Mar 12 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: John MacIntyre | last post by:
Hi, I have a page with a series of child pages loaded into an iframe. When I move from page to page, I store an object containing the child's control data in a variable on the main page, then...
7
by: chirs | last post by:
Hi, These 2 lines caused an error in IE5. The error is "Function expected". Why? var d=new Date(); document.write(d instanceof Object + "<br>"); Thanks.
5
by: rua17 | last post by:
Can anyone tell me what's the command in c# that is equivalent to the instanceOf in java? thanks
4
by: Sean Inglis | last post by:
Well bizarre to me, anyway. I've distilled it down to two small files: testtop.htm =============================== <html> <head> <script language="Javascript">
2
by: System Administrator | last post by:
Hi: Can someone explain why BOTH of the following are true ? Function instanceof Object //true Object instanceof Function //true (Either one or the other should be true, not both,...
15
by: shana07 | last post by:
Can I have many instanceof code for one 'if' ? for example..... if(checkMe instanceof AInstruction instanceof LocVariableInst) What I am trying to say is, these instanceof are refer to these...
13
by: Robert | last post by:
Hi, Is it possible to intercept a call to "instanceof" and return something different? I wanted to see if it was possible to implement something like multiple inheritance, especially to use...
30
by: kj | last post by:
My book (Flanagan's JavaScript: The Definitive Guide, 5th ed.) implies on page 111 that the following two constructs are equivalent: ( x.constructor == Foo ) and ( x instanceof Foo ) The...
3
by: whitelined | last post by:
Hi, How widely supported is the instanceof operator? Is there an alternative to seeing if an object is an instance of a constructor? Many thanks Regards Aaron
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.