0

I am new to javascript. Today I learned about prototypes. I want to know why dunder prototypes are discouraged and why we use Object.getPrototypeOf in its place? I only noticed the difference in the way we write them.

Dunder prototype - objectname.prototype.function

new way that I learned - Object.getPrototypeOf(objectname)

  • `objectname.prototype` is **not** "dunder prototype". That term refers to `objectname.__proto__`. And [it's deprecated for good reasons](https://stackoverflow.com/a/36061819/1048572) (such as [prototype pollution attacks](https://codeburst.io/what-is-prototype-pollution-49482fc4b638)) - it never was a good idea to access the prototype chain as if it was an ordinary property. – Bergi May 21 '21 at 12:41

1 Answers1

0

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/proto

Warning: While Object.prototype.proto is supported today in most browsers, its existence and exact behavior has only been standardized in the ECMAScript 2015 specification as a legacy feature to ensure compatibility for web browsers. For better support, use Object.getPrototypeOf() instead.

TKoL
  • 13,158
  • 3
  • 39
  • 73
  • Okay, just went through this. But, still not clear on the why of the above. – Rajat Sharma May 21 '21 at 12:37
  • @RajatSharma I believe it's just that it's only supported as a legacy feature and it's not necessarily available in all browsers. – TKoL May 24 '21 at 14:13