3 ms·
`__proto__` doesn't necessarily equal `.prototype`. var foo = Object.create(null) //now foo.prototype and foo.__proto__ are both undefined foo.prot
by hajile 6y ago
`__proto__` doesn't necessarily equal `.prototype`.
var foo = Object.create(null)
//now foo.prototype and foo.__proto__ are both undefined
foo.prototype = {abc:123}
//foo.__proto__ is still undefined. Need to use Object.setPrototypeOf()
In older JS code, I've seen people trying to abuse prototypes. One result in this kind of thing is often retaining references to those hidden `__proto__` leading to memory leaks.
Also, `__proto__` is deprecated. If you're writing JS, you should be using `.getPrototypeOf()` instead.
> Could you please expand this part? "Primitive" has specific meaning in JavaScript.
var fn = function () {}
fn.bar = "abc"
Object.keys(fn) //=> ["bar"]
//likewise
(1).__proto__ === Number.prototype //=> true
JS is torn on the idea of whether something is primitive or an object. You see this (for example) in Typescript with the primitive number being different from the Number type which represents a number object. To get at the primitive, you must actually call `.valueOf()` which returns the primitive in question. Meanwhile, you can attach your own properties to the function object -- a fact exploited by many, many libraries including modern ones like React. You can also add your own `.valueOf()` to allow your code to better interact with JS operators, but I believe that to pretty much always be a bad practice.
- sergeykish 6y agoYes, I know about null [[Prototype]] and who is owner of prototype and __proto__. I like how it is not real property var foo = { __proto__: null } foo.__proto__ undefined As you can see I do not store it and do not modify it, it is assertion, kind of instanceof. > Also, `__proto__` is deprecated Object.getPrototypeOf would be too verbose in this example. I could have defined Object.defineProperty(Object.prototype, "proto", { get() { Object.getPrototypeOf(this) } }) but why bother? We both know that I meant [[Prototype]]. > Primitive > fn.bar = "abc" is syntactic sugar for fn["bar"] = "abc" do not follow. > (1).__proto__ === Number.prototype number is primitive typeof 1 "number" 1 instanceof Number false number is wrapped in Number when we access property Number.prototype.foo = function () { return this } 1..foo() //Number {1} typeof 1..foo() //"object" 1..foo() instanceof Number //true Function is not primitive typeof Math.max "function" Math.max instanceof Function true Math.max instanceof Object true it is an object, we can attach properties to object. As I remember valueOf is complicated by hints, exposed in JavaScript [1]. I've played with removal delete Array.prototype.toString delete Object.prototype.toString Uncaught TypeError: Cannot convert object to primitive value Unfortunately it converts to string and than converts string to number. delete Array.prototype.valueOf delete Object.prototype.valueOf +[] //0 [1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toPrimitive https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...