0

From this question, I can understand that the API interfaces and constructor functions are two distinct concepts.

In javascript there's no notion of interfaces, so how are these interfaces used in javascript?

I mean they look like constructor functions in my eyes typeof(Node) === 'function' // true

But they are not, we cannot use them to construct elements

So how does javascript use these interfaces or how does it implement them while it doesn't have the concept of interfaces in its core like java or C#?

It has a behaviour with them that emulates that of constructors someElement instanceof HTMLElement // true and they have prototypes like constructors and you can extend them with methods using these prototypes but they aren't constructors because they can't be used to construct dom elements.

No One
  • 57
  • 5

1 Answers1

1

It could be a constructor that fails at construction:

 function Node() {
   throw new TypeError("Illegal constructor");
 }
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • maybe a good approach to the point, but can you explain why when we call `HTMLElement` without new, it throws an error which is not `TypeError: Illegal Constructor`? How can it detect that it's not used as a constructor? – No One Aug 04 '18 at 15:35
  • 1
    @noOne cause `new.target` isnt set. – Jonas Wilms Aug 04 '18 at 15:37