0% found this document useful (0 votes)
26 views1 page

Understanding Classes in Javascript

Classes in JavaScript provide a cleaner syntax compared to constructor functions and prototypes, allowing developers to mimic object-oriented patterns in a more familiar way. Classes are essentially syntactic sugar over JavaScript's prototype-based inheritance. A JavaScript class is a type of function that is declared with the class keyword, making it easier for developers to transition between class-based and prototype-based languages.

Uploaded by

Milena Mijović
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views1 page

Understanding Classes in Javascript

Classes in JavaScript provide a cleaner syntax compared to constructor functions and prototypes, allowing developers to mimic object-oriented patterns in a more familiar way. Classes are essentially syntactic sugar over JavaScript's prototype-based inheritance. A JavaScript class is a type of function that is declared with the class keyword, making it easier for developers to transition between class-based and prototype-based languages.

Uploaded by

Milena Mijović
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Understanding Classes in JavaScript

Introduction
JavaScript is a prototype-based language, and every object in JavaScript has a hidden
internal property called [[Prototype]] that can be used to extend object properties and
methods. You can read more about prototypes in our Understanding Prototypes and
Inheritance in JavaScript tutorial.

Until recently, industrious developers used constructor functions to mimic an object-

oriented design pattern in JavaScript. The language specification ECMAScript 2015, often

referred to as ES6, introduced classes to the JavaScript language. Classes in JavaScript do

not actually offer additional functionality, and are often described as providing “syntactical

sugar” over prototypes and inheritance in that they offer a cleaner and more elegant

syntax. Because other programming languages use classes, the class syntax in JavaScript

makes it more straightforward for developers to move between languages.

Classes Are Functions


A JavaScript class is a type of function. Classes are declared with the class keyword. We
will use function expression syntax to initialize a function and class expression syntax to
initialize a class.

// Initializing a function with a function expression


const x = function() {}

Copy
// Initializing a class with a class expression
const y = class {}

Copy
We can access the [[Prototype]] of an object using
the Object.getPrototypeOf() method. Let’s use that to test the empty function we
created.

You might also like