2

So I am aware about prototype and proto

function Vehicle()
{
this.brand=brand
}

const vehicle =new Vehicle("Ford")
console.log(vehicle.__proto__===Vehicle.prototype) // true

and when I print this I get this

enter image description here

Why I cant see the proto property ? .Is __proto__ === [[Prototype]] ?

gANDALF
  • 360
  • 4
  • 21
  • Yes, it is..... – Wiktor Zychla Sep 13 '21 at 05:28
  • 3
    There is no `__proto__` property on these objects. It always was an [internal property](https://stackoverflow.com/q/11003021/1048572) that just was displayed as such. Now the devtools display it more consistently like other internal properties with `[[…]]` syntax. – Bergi Sep 13 '21 at 05:28
  • [https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Object/proto](https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Object/proto) – Ricky Mo Sep 13 '21 at 05:29
  • @Bergi yeah ok so why this vehicle.[[Prototype]] doesn't work – gANDALF Sep 13 '21 at 05:30
  • @WiktorZychla but it used to be visible before ,and I remember it correctly – gANDALF Sep 13 '21 at 05:31
  • 1
    @gANDALF Because it's an *internal* property. `vehicle.__proto__` works (but is deprecated in favour of `Object.getPrototype`) because it's a getter inherited from `Object.prototype`. You can find it when expanding the prototype chain one step further. – Bergi Sep 13 '21 at 05:32
  • Umm understood ,so `[[Prototype]]` is just for viewing ? we can't access it anyhow in the script? @Bergi – gANDALF Sep 13 '21 at 05:45
  • 2
    @gANDALF "*so why this vehicle.[[Prototype]] doesn't work*" two reasons: 1. it's not a correct syntax 2. It's only *displayed* as `[[Prototype]]` it's not actually called that. It shows *the* prototype as `[[Prototype]]`. Don't take the console representation as gospel. Consoles can format data however they like. – VLAZ Sep 13 '21 at 05:46
  • @VLAZ but still vehicle.prototype won't work too ,only vehicle.__proto__ will work ,and __proto__ is depracted too ,its bit confusing :D – gANDALF Sep 13 '21 at 05:49
  • 3
    @gANDALF `.prototype` only exists on the constructor, not on instances. That has always been the case. `__proto__` was an old way to get the prototype of an instance but it has been deprecated. `Object.getPrototypeOf()` is the correct way to get the same data. See also [Object.getPrototypeOf() vs .prototype](https://stackoverflow.com/q/38740610) – VLAZ Sep 13 '21 at 05:51

0 Answers0