Checking for method inheritance via the === operator ? [i am confused]

  • Thread starter System Administrator
  • Start date
S

System Administrator

function a() { this.x = '1'; } ;
x1 = new a();

a.toString === Object.toString; //true
x1.toString === Object.toString; //false
x1.toString === a.toString; //false

Can someone post a cogent explanation as to why the first
is true and the other two are false ?

Best regards,
--j
 
R

Richard Cornford

System said:
function a() { this.x = '1'; } ;
x1 = new a();

a.toString === Object.toString; //true
x1.toString === Object.toString; //false
x1.toString === a.toString; //false

Can someone post a cogent explanation as to why the first
is true and the other two are false ?

Object - is a function, the function that is used as a constructor in -
new Object() - to create a new object. As a result its - toString -
method is the one inherited from - Function.prototype.toString -. Your -
a - constructor is also a function, and also inherits its - toString -
method from - Function.prototype.toString -. Thus - a.toString ===
Object.toString - should be expected to be true as the two functions
inherited the same function as their - toString - methods.

Your - x1 - is an object (as constructed with - new a() -). It inherits
its - toString - method from - Object prototype.toString -, which is not
the same method as - Function.prototype.toString - and so not the same
method as either - Object.toString - or - a.toString -.

Richard.
 
J

java

Richard:

Thanks for the reply. You have mad skillz :)

As an aside, your explanation implies:

a.prototype.toString === Object.prototype.toString
a.toString === Function.prototype.toString

are both true and indeed they are.

Thanks,
--j
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top