0

I am fully aware of other very similar posts about object.create but I really cannot arrive to answer on this particular question. I saw a code once where it looks like it was using object.create to create new prototype and then adding back to constructor

function blah(){
   // typical constructor
}

blah.prototype = Object.create(somethingelse.prototype);
blah.prototype.constructor = blah;

Something like that .. and I tried that in chrome dev tool but did not see them as equal.. Now trying to see what that code I did see was trying to accomplish? I assume there is more difference in Object create and new(than just missing constructor?)

function blah(){
 //...
}
undefined
function somethingelse(){
 //
}
undefined
somethingelse.prototype = {
    // something else
}
Object {}
blah.prototype = Object.create(somethingelse.prototype);
Object {}
blah.prototype.constructor = blah;
blah(){
 //...
}
blah == somethingelse;
false
user3502374
  • 781
  • 1
  • 4
  • 12
  • `blah` and `somethingelse` are two different functions. Why do you think they both would be equal/same? – thefourtheye Dec 25 '15 at 06:41
  • actually I meant to post the one w/ new .. doing it now – user3502374 Dec 25 '15 at 06:44
  • actually I was confused and now I clearly see what that code did.It wanted to get prototype out of something else but still add back it's own constructor(which is different from somethingelse's). – user3502374 Dec 25 '15 at 06:49
  • What are you actually trying to accomplish? Area you trying to inherit from somethingelse? Or just create an instance of somethingelse? Do you understand the difference between those two? – jfriend00 Dec 25 '15 at 06:58
  • I guess that code was trying to inherit from some other's prototype but still be able to add back it's own constructor(which is not possible if one were to use the 'new' to build the object). – user3502374 Dec 25 '15 at 07:05
  • I'm guessing that you should read [What it the significance of the Javascript constructor property?](http://stackoverflow.com/questions/4012998/what-it-the-significance-of-the-javascript-constructor-property) and [When is the `.constructor` property of an object actually used](http://stackoverflow.com/questions/23186731/when-is-the-constructor-property-of-an-object-actually-used). It's barely used and doesn't generally affect the behavior of the object at all. When declaring a derived prototype, it is customary to set it to the new constructor, but it is hardly ever used in real practice. – jfriend00 Dec 25 '15 at 07:07
  • Thank you. Checking them out – user3502374 Dec 25 '15 at 07:13

1 Answers1

0

You've created two different objects (blah and something). I think you are confused about what inheritance means.

when you compare (==) 2 objects the reference is being evaluated - and it is different.

chenop
  • 4,743
  • 4
  • 41
  • 65