JavaScript Classes: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 10: Line 10:
A JavaScript class is a syntactical superstructure in top of the language's existing prototype-based inheritance, and it does not introduce a new object-oriented inheritance model in JavaScript. A class is a special [[JavaScript_Functions|function]], so like in the functions' case, a class syntax has two components: [[#Declaration|declarations]] and [[#Class_Expressions|expressions]].  
A JavaScript class is a syntactical superstructure in top of the language's existing prototype-based inheritance, and it does not introduce a new object-oriented inheritance model in JavaScript. A class is a special [[JavaScript_Functions|function]], so like in the functions' case, a class syntax has two components: [[#Declaration|declarations]] and [[#Class_Expressions|expressions]].  


=Class Hoisting==
=Class Hoisting=


An important difference between function declaration and class declaration is that function declaration and [[JavaScript Hoisting|hoisted]], while the class declarations are not: you first need to declare the class and then access it, otherwise a ReferenceError is thrown.
An important difference between function declaration and class declaration is that function declaration and [[JavaScript Hoisting|hoisted]], while the class declarations are not: you first need to declare the class and then access it, otherwise a ReferenceError is thrown.

Revision as of 22:53, 21 January 2020

External

Internal

Overview

A JavaScript class is a syntactical superstructure in top of the language's existing prototype-based inheritance, and it does not introduce a new object-oriented inheritance model in JavaScript. A class is a special function, so like in the functions' case, a class syntax has two components: declarations and expressions.

Class Hoisting

An important difference between function declaration and class declaration is that function declaration and hoisted, while the class declarations are not: you first need to declare the class and then access it, otherwise a ReferenceError is thrown.

Declaration

Use the class keyword:

class Simplest {
  constructor(content) {
    this.content = content;
  }
}

Class Expression