Wednesday, 20 November 2019

Why hoisting works differently for classes?

The term hoisting in JavaScript class reference is: you first need to declare your class and then access it, otherwise code like the following will throw a ReferenceError:

const p = new Rectangle(); // ReferenceError
class Rectangle {}

In other references: all variable declarations are hoisted to the top of their scope regardless of where the actual declaration has been made.

However MDN says:

Hoisting is a term you will not find used in any normative specification prose prior to ECMAScript® 2015 Language Specification. Hoisting was thought up as a general way of thinking about how execution contexts (specifically the creation and execution phases) work in JavaScript. However, the concept can be a little confusing at first.

It looks hoist means a lot what I understand. However, it is weird to use something before declaration, but why it work differently for classes whereas class is also a type of function in JavaScript.



from Why hoisting works differently for classes?

No comments:

Post a Comment