|
|
|
|
|
by hajile
2073 days 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. |
|
> Also, `__proto__` is deprecated
Object.getPrototypeOf would be too verbose in this example. I could have defined
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
number is wrapped in Number when we access property Function is not primitive 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
Unfortunately it converts to string and than converts string to number. [1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...