0

Recently I was going deep dive into the inheritance. As most of us know that almost every datatype is made of an Object, hence they will have access to the Object's prototype. I can check this by doing the following code.

"Mohit".__proto__.__proto__ === Object.prototype
return true;

This is perfectly fine. I know how this is working. But I was surprised when I saw this code.

console.log(String.__proto__)
prints => ƒ () { [native code] }


String.__proto__ === Array.__proto__
return true;

What is this? Why is it returning native function code? Why every dataType's class proto returns this native function code?

  • 2
    Both of them are functions, thus instances of `Function`. That's really all about it, no deeper meaning. Nor anything you didn't really show, as it's just `String.__proto__ === Function.prototype` similar to `"Mohit".__proto__.__proto__ === Object.prototype` – VLAZ Oct 02 '22 at 14:45
  • Thanks for the quick response and help @VLAZ . Your answer is correct. – Mohit Singh Negi Oct 02 '22 at 16:02
  • If `console.log` output appears useless, try `console.dir` instead – Bergi Oct 02 '22 at 18:00
  • 2
    Remember that `__proto__` is deprecated! Always use `Object.getPrototypeOf` instead. – Bergi Oct 02 '22 at 18:02
  • Created a diagram on basis of my understanding https://viewer.diagrams.net/index.html?tags=%7B%7D&highlight=0000ff&edit=_blank&layers=1&nav=1&title=Protoype%20Inheritance.drawio#Uhttps%3A%2F%2Fdrive.google.com%2Fuc%3Fid%3D1GL2koYzGpFmwgXSMDWEsYapfy4v3CH-W%26export%3Ddownload – Mohit Singh Negi Oct 03 '22 at 18:16
  • @MohitSinghNegi I don't know what those grey boxes are supposed to mean but they look wrong – Bergi Oct 03 '22 at 18:40
  • 1
    @MohitSinghNegi Have a look at [this](https://stackoverflow.com/questions/29155986/javascript-diagram-to-explain-inheritance-proto-and-prototype) and the other questions linked from there – Bergi Oct 03 '22 at 18:42

0 Answers0