MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Class Syntax & Constructor Methods0%
Study Materials
JavaScript for AdvancedProgress 0%
1.1Higher-Order Functions & Currying1.2Function Composition & Pipelining1.3Memoization & Pure Functions1.4Execution Context & Call Stack Deep Dive1.5Scope Chain & Lexical Environment
2.1Class Syntax & Constructor Methods2.2Getters, Setters & Static Methods2.3Private Fields & Methods (#field)2.4Prototypal Inheritance vs Class Inheritance2.5Mixins & Object Composition
HomeTutorialsJavaScript for AdvancedClass Syntax & Constructor Methods
Getters, Setters & Static Methods

Class Syntax & Constructor Methods

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study Materials

Introduced in ECMAScript 2015 (ES6), the class syntax provides a clean, declarative syntax over JavaScript's existing prototype-based inheritance model. While classes in JavaScript are syntactic sugar over prototypes, they introduce strict semantics, constructor encapsulation, and class-level invariants.


1. Class Declaration & The constructor Method

A class defines the blueprint for objects. The constructor() method is a special function that runs automatically whenever a new instance is created via the new operator:

1class BankAccount {
2 // Constructor: Initializes instance properties
3 constructor(accountHolder, initialBalance = 0) {
4 this.accountHolder = accountHolder;
5 this.balance = initialBalance;
6 this.createdAt = new Date();
7 }
8 
9 // Instance Method (Added to BankAccount.prototype)
10 deposit(amount) {
11 if (amount <= 0) throw new Error('Deposit amount must be positive.');
12 this.balance += amount;
13 return this.balance;
14 }
15 
16 withdraw(amount) {
17 if (amount > this.balance) throw new Error('Insufficient funds.');
18 this.balance -= amount;
19 return this.balance;
20 }
21}
22 
23// Instantiation
24const myAccount = new BankAccount('Sarah Connor', 1000);
25myAccount.deposit(500);
26console.log(`Balance: $${myAccount.balance}`); // Balance: $1500

2. Key Differences Between Classes & Constructor Functions

FeatureES6 ClassConstructor Function (function Account())
Strict ModeAutomatically runs in 'use strict'Non-strict unless explicitly declared
Invocation without newThrows TypeError immediatelyFails silently and pollutes global window
HoistingTemporal Dead Zone (Cannot instantiate before declaration)Hoisted (Can be called before definition)
Method EnumerabilityMethods on prototype are non-enumerablePrototype methods are enumerable by default
1// Classes CANNOT be called without 'new':
2// BankAccount('Alex', 100); // TypeError: Class constructor BankAccount cannot be invoked without 'new'

3. Subclassing with extends and super()

To inherit from an existing class, use extends. The subclass constructor must invoke super() before accessing this:

1class PremiumAccount extends BankAccount {
2 constructor(accountHolder, initialBalance, cashbackRate = 0.02) {
3 // MUST call super() before accessing 'this'!
4 super(accountHolder, initialBalance);
5 this.cashbackRate = cashbackRate;
6 }
7 
8 // Method overriding
9 deposit(amount) {
10 const cashback = amount * this.cashbackRate;
11 const totalDeposit = amount + cashback;
12 console.log(`Bonus Cashback Earned: $${cashback}`);
13 return super.deposit(totalDeposit); // Invoke parent method via super
14 }
15}
16 
17const vip = new PremiumAccount('John Connor', 5000);
18vip.deposit(1000); // Bonus Cashback: $20, Balance: $6020
Critical Rule: In a derived class constructor, accessing this before calling super() results in a ReferenceError: Must call super constructor in derived class before accessing 'this'.

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What happens if you attempt to instantiate an ES6 class without the new keyword (e.g. BankAccount())?
Question 2
What must be called inside a derived subclass constructor before accessing the this keyword?
Question 3
Where are standard methods declared inside a class body (like deposit()) stored in memory?
Question 4
Are ES6 classes hoisted like traditional function declarations?
Question 5
How do prototype methods created via class syntax differ in enumerability from traditional prototype functions?
Next Lesson

Getters, Setters & Static Methods

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Scope Chain & Lexical EnvironmentGetters, Setters & Static Methods

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master JavaScript for Advanced with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext