Abra Cadabra
Abra Cadabra

Reputation: 161

Why is the JS constructor function create functions from strings?

As I understand the .constructor value is a Function() for classes.

In short, why constructor attribute is a Function and whta the rational about it?

Example: Set.constructor -> Function()

Upvotes: 0

Views: 22

Answers (1)

CertainPerformance
CertainPerformance

Reputation: 371019

Classes are (very) special kinds of functions; Set inherits from Function.prototype.

Function.prototype has a constructor property - that's what Set.constructor refers to. (Set does not have an own constructor property - it's only inherited.)

The constructor of Function.prototype is what you can call to create a Function instance - which, in this special case, is just Function.

console.log(Set.constructor === Function);

So

Set.constructor('alert(1);')

is equivalent to

Function('alert(1);')

Upvotes: 1

Related Questions