3

I was reading on the differences between the __proto__ and the prototype object, but when I tried to access the __proto__ of an instance of an object, it returned me undefined.

Following is the code I wrote:

function Student() {

}

var student = new Student();

student.constructor // works well returns function Student() {}

student.__proto__ // returns undefined.

I was referring this blog but I saw other blogs too that show the same. We never get the prototype on the instance of an object but __proto__ object instead that was created using the prototype property.

Am I missing something or __proto__ has been removed entirely? I've tested this on Chrome version 40.0.2214.94 on Linux.

Any help appreciated. Thanks!

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
I_Debug_Everything
  • 3,776
  • 4
  • 36
  • 48
  • Have you tried on other browsers? If anything, __proto__ is only recently standardized in ES6, not removed. – Lim H. Mar 14 '15 at 19:12
  • Create a snippet here and see what it gives :) – Icepickle Mar 14 '15 at 19:17
  • Rejoice! Hail this browser who removed this insufferable property! @OP: Use [`Object.getPrototypeOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getPrototypeOf) – Bergi Jun 25 '15 at 09:17

1 Answers1

0

The property __proto__ is not standard in JavaScript versions below ECMA 6 so you cannot expect every broser to support that, or an equal behavior in every browser yet. Its best to avoid using proto at all. You can read about it here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/proto

And here is an interesting comment about that by brendan eich: https://brendaneich.com/2011/01/harmony-of-my-dreams/ EDIT:

Object.setPrototypeOf is not going to happen. Writable proto is a giant pain to implement (must serialize to cycle-check) and it creates all sorts of type-confusion hazards. You may think you want it as a low-level sharp instrument. JS is not that language. Higher-level forms for classes and mixins seem much better and do not involve such sharp edges.

treeno
  • 2,510
  • 1
  • 20
  • 36
  • Uh, sure that's the correct link? That https://brendaneich.com/2011/01/harmony-of-my-dreams/ article says absolutely nothing about `__proto__`. – Bergi Jun 25 '15 at 09:22
  • yes: search for `__proto__`, it's an older comment, you need to scroll down a little – treeno Jun 25 '15 at 09:24
  • ah, that specific comment. Yes, [mutating the \[\[prototype\]\] is bad](http://stackoverflow.com/a/23809148/1048572), but that's not what OP is asking about. – Bergi Jun 25 '15 at 09:30
  • True, I thought it would give some background information about why using `__proto__` is controversial. – treeno Jun 25 '15 at 09:40