Why does Element.__proto__ === Node
return true? Every function's __proto__
value is function () {}
. How can I make a function that has for a __proto__
value another function?
Asked
Active
Viewed 72 times
-1

LearningMath
- 851
- 4
- 15
- 38
1 Answers
0
Every function's
__proto__
value isfunction () {}
.
No, not every function inherits directly from Function.prototype
.
How can I make a function that has for a
__proto__
value another function?
Using class extends …
is the most common pattern - the subclass constructor will inherit static properties directly from the parent class constructor (next to the prototype objects inheriting from each other). You can also do it by using Object.setPrototypeOf
or by inheriting from Function
.

Bergi
- 630,263
- 148
- 957
- 1,375
-
Can it be made without using classes (because they are new in Javascript, I suppose it can be made differently). Also, what's the point in making a function's __proto__ another function, not Function.prototype? What are some usages and examples using this? – LearningMath Jul 26 '18 at 15:59
-
1@learningMath in classes they are used because then `super()` will point to the superclasses constructor and static properties will be inherited – Jonas Wilms Jul 26 '18 at 16:09
-
Can this be done using this code: `Child.__proto__ = Object.create(Parent)`? – LearningMath Jul 26 '18 at 16:13
-
@LearningMath You can also do it by using [`Object.setPrototypeOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/setPrototypeOf) or by [inheriting from `Function`](https://stackoverflow.com/q/36871299/1048572). Notice that `Object.create` isn't necessary, and also can't create functions. Also, `__proto__` is deprecated, don't use it. – Bergi Jul 26 '18 at 16:30
-
@Bergi Can you give an example from the DOM functions where a function inherits methods from its `__proto__` function? Like `Node` and `Element` here, but I don't see any methods inherited. – LearningMath Jul 26 '18 at 16:38
-
@LearningMath `Element` inherits all the static properties of `Node` such as `Node.ELEMENT_NODE` – Bergi Jul 26 '18 at 16:44
-
Thanks. But why are they shown on the Element.prototype object too in dev tools? – LearningMath Jul 26 '18 at 16:48
-
@LearningMath I guess because they *are* duplicated there because of IE compatibility afaik. – Bergi Jul 26 '18 at 16:49
-
@Bergi Also, why is there written `Node` only on one static property from the right(which means it comes from Node) and not on the others? Does it mean that all the properties from that one, to the next where it says `EvenTarget` on the right come from Node? – LearningMath Jul 26 '18 at 16:53