0

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 is it work differently for classes whereas class is also a type of function in JavaScript.

Kunj
  • 1,980
  • 2
  • 22
  • 34
  • Refer this MDN article for more information:https://developer.mozilla.org/en-US/docs/Glossary/Hoisting – ashish bandiwar Sep 25 '19 at 12:33
  • Hi @ashishbandiwar , thanks for reply. The first para of this article is pointed to answer. – Kunj Sep 25 '19 at 12:54
  • Check the accepted answer in [Are variables declared with let or const not hoisted in ES6?](https://stackoverflow.com/questions/31219420) – adiga Nov 18 '19 at 16:25

5 Answers5

4

Actually classes are hoisted as the variable binding is available in the whole scope, but they are not initialised.

Ref: Are variables declared with let or const not hoisted in ES6?

Mosè Raguzzini
  • 15,399
  • 1
  • 31
  • 43
  • ..are hoisted ...but they are not initialised.. thanks to explain. The way of clearing doubt is now open... – Kunj Nov 19 '19 at 05:54
1

Hoisting doesn't work for classes. Only for variables and functions.
It is possible to initialize a variable or call a function before it's declared.
See the following snippet. It doesn't throw error but logs "Foo" onto the console.

var p = new rectangle();
function rectangle() { console.log("Foo") }
ElmoVanKielmo
  • 10,907
  • 2
  • 32
  • 46
  • Hi @ElmoVanKielmo appreciate you know us. This is the way of clearing doubts, however some find it not useful. – Kunj Sep 25 '19 at 12:45
  • 1
    Technically, `class`es, `const`s and `let`s are all hoisted. The behavior is just different, when you try to use a variable before its declaration (i.e. TDZ error vs. `undefined` or the declared function). – Sebastian Simon Sep 25 '19 at 12:48
0

Hoisting in Javascript is putting declarations into memory before it executes any code segment is that it allows you to use a function before you declare it in your code.

For example:

foo("bar");

function foo(name) {
  console.log(name);
}

Javascript hoisting allows you to call the function before it has been declared. NOTE: Javascript doesn't have hoisting for classes.

protestator
  • 311
  • 1
  • 15
0

Class is Reference type

const will only work with Value type data

so you can't use Class as const

Rectangle p = new Rectangle(); 
class static Rectangle {}
Ravi Makwana
  • 2,782
  • 1
  • 29
  • 41
0

Reference 1: https://scotch.io/tutorials/understanding-hoisting-in-javascript

JavaScript classes too can be loosely classified either as:

  • Class declarations
  • Class expressions

Class declarations

Much like their function counterparts, JavaScript class declarations are hoisted. However, they remain uninitialised until evaluation. This effectively means that you have to declare a class before you can use it.

var Frodo = new Hobbit();
Frodo.height = 100;
Frodo.weight = 300;
console.log(Frodo); // Output: ReferenceError: can't access lexical declaration `Hobbit' before initialization

class Hobbit {
  constructor(height, weight) {
    this.height = height;
    this.weight = weight;
  }
}

So, as far as class declarations go, to access the class declaration, you have to declare first.

Class expressions

Much like their function counterparts, class expressions are not hoisted. Here's an example with the un-named or anonymous variant of the class expression.

var Square = new Polygon();
Square.height = 10;
Square.width = 10;
console.log(Square); // Output: TypeError: Polygon is not a constructor

var Polygon = class {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
};

There's a bit of an argument to be made as to whether JavaScript es6 let, const variables and classes are actually hoisted, roughly hoisted or not hoisted. Some argue that they are actually hoisted but uninitialised whilst some argue that they are not hoisted at all. Therefore, we should make it a habit to declare and initialise JavaScript variables before use. Using strict mode in JavaScript es5 can help expose undeclared variables.

P.S.: Other references to be continued...

Kunj
  • 1,980
  • 2
  • 22
  • 34