0% found this document useful (0 votes)
11 views

JavaScript Classes Cheatsheet

This document provides an overview of key concepts for working with classes in JavaScript, including: - Static methods that can be called on a class itself rather than an instance. - Classes that specify shared properties and methods for objects instantiated from the class. - Constructor methods that are called when an object is created to set initial values. - Child classes that can extend parent classes and inherit their properties and methods.
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)
11 views

JavaScript Classes Cheatsheet

This document provides an overview of key concepts for working with classes in JavaScript, including: - Static methods that can be called on a class itself rather than an instance. - Classes that specify shared properties and methods for objects instantiated from the class. - Constructor methods that are called when an object is created to set initial values. - Child classes that can extend parent classes and inherit their properties and methods.
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/ 4

1/3/24, 1:09 PM JavaScript Iterators, Objects, and Classes: Classes Cheatsheet | Codecademy

Cheatsheets / JavaScript Iterators, Objects, and Classes

Classes

Static Methods

Within a JavaScript class, the static keyword defines a class Dog {


static method for a class. Static methods are not called
constructor(name) {
on individual instances of the class, but are called on
the class itself. Therefore, they tend to be general this._name = name;
(utility) methods. }

introduce() {
console.log('This is ' + this._name +
' !');
}

// A static method
static bark() {
console.log('Woof!');
}
}

const myDog = new Dog('Buster');


myDog.introduce();

// Calling the static method


Dog.bark();

https://www.codecademy.com/learn/bwa-javascript-iterators-objects-and-classes/modules/learn-javascript-classes/cheatsheet 1/4
1/3/24, 1:09 PM JavaScript Iterators, Objects, and Classes: Classes Cheatsheet | Codecademy

Class

JavaScript supports the concept of classes as a syntax class Song {


for creating objects. Classes specify the shared
constructor() {
properties and methods that objects produced from
the class will have. this.title;
When an object is created based on the class, the new this.author;
object is referred to as an instance of the class. New
}
instances are created using the new keyword.
The code sample shows a class that represents a
Song . A new object called mySong is created play() {
underneath and the .play() method on the class is
console.log('Song playing!');
called. The result would be the text Song playing!
printed in the console. }
}

const mySong = new Song();


mySong.play();

Class Constructor

Classes can have a constructor method. This is a class Song {


special method that is called when the object is
constructor(title, artist) {
created (instantiated). Constructor methods are usually
used to set initial values for the object. this.title = title;
this.artist = artist;
}
}

const mySong = new Song('Bohemian


Rhapsody', 'Queen');
console.log(mySong.title);

https://www.codecademy.com/learn/bwa-javascript-iterators-objects-and-classes/modules/learn-javascript-classes/cheatsheet 2/4
1/3/24, 1:09 PM JavaScript Iterators, Objects, and Classes: Classes Cheatsheet | Codecademy

Class Methods

Properties in objects are separated using commas. This class Song {


is not the case when using the class syntax. Methods
play() {
in classes do not have any separators between them.
console.log('Playing!');
}

stop() {
console.log('Stopping!');
}
}

extends

JavaScript classes support the concept of inheritance // Parent class


— a child class can extend a parent class. This is
class Media {
accomplished by using the extends keyword as part
of the class definition. constructor(info) {
Child classes have access to all of the instance this.publishDate = info.publishDate;
properties and methods of the parent class. They can
this.name = info.name;
add their own properties and methods in addition to
those. A child class constructor calls the parent class }
constructor using the super() method. }

// Child class
class Song extends Media {
constructor(songData) {
super(songData);
this.artist = songData.artist;
}
}

const mySong = new Song({


artist: 'Queen',
name: 'Bohemian Rhapsody',
publishDate: 1975
});

https://www.codecademy.com/learn/bwa-javascript-iterators-objects-and-classes/modules/learn-javascript-classes/cheatsheet 3/4
1/3/24, 1:09 PM JavaScript Iterators, Objects, and Classes: Classes Cheatsheet | Codecademy

Print Share

https://www.codecademy.com/learn/bwa-javascript-iterators-objects-and-classes/modules/learn-javascript-classes/cheatsheet 4/4

You might also like