Classes and Constructor
1. ES6 class
Implementation
How It Works
- Class Declaration (
User
):- The
constructor
initializesusername
,email
, andpassword
. encryptPassword()
appends"abc"
to the password.changeUsername()
convertsusername
to uppercase.
- The
class User {
constructor(username, email, password) {
this.username = username;
this.email = email;
this.password = password;
}
encryptPassword() {
return `${this.password}abc`;
}
changeUsername() {
return `${this.username.toUpperCase()}`;
}
}
const chai = new User('chai', 'chai@gmail.com', '123');
console.log(chai.encryptPassword()); // Output: "123abc"
console.log(chai.changeUsername()); // Output: "CHAI"
2. Behind the Scenes: Prototype-Based Approach
How It Works
- Constructor Function (
User
):- Similar to the class, it initializes properties inside the function.
- Prototype Methods (
encryptPassword
,changeUsername
):- These methods are attached to
User.prototype
, so they are shared across instances instead of being recreated for each object.
- These methods are attached to
function User(username, email, password) {
this.username = username;
this.email = email;
this.password = password;
}
User.prototype.encryptPassword = function () {
return `${this.password}abc`;
};
User.prototype.changeUsername = function () {
return `${this.username.toUpperCase()}`;
};
const tea = new User('tea', 'tea@gmail.com', '123');
console.log(tea.encryptPassword()); // Output: "123abc"
console.log(tea.changeUsername()); // Output: "TEA"
📌 Key Differences: Class vs. Prototype
Feature | ES6 Class | Prototype-Based Approach |
---|---|---|
Syntax | Uses class keyword | Uses constructor function + prototype |
Method Storage | Methods are on prototype | Explicitly defined on User.prototype |
Instantiation | new User(...) | new User(...) (same) |
Readability | Cleaner, more structured | More verbose, traditional |
📌 Trivia and Additional Notes
-
Classes are syntactic sugar
- The ES6 class is just a cleaner way to define objects; behind the scenes, JavaScript still uses prototypes.
- The
class
syntax improves readability without changing the behavior.
-
Prototype Methods Save Memory
- Instead of creating methods inside the constructor (which would duplicate them for each instance), prototype methods allow all instances to share the same function reference.
-
Difference in
this
Binding- In
class
, methods are automatically bound to the instance when used withnew
. - In prototype-based functions, methods need explicit
this
references.
- In
static
in JavaScript
1. Understanding the User
Class
class User {
constructor(username) {
this.username = username;
}
logMe() {
console.log(`Username: ${this.username}`);
}
static createId() {
return `123`;
}
}
How It Works
constructor(username)
: Initializes theusername
property.logMe()
: A regular instance method that logs theusername
.static createId()
: A static method that belongs to the class itself, not instances.
2. Instantiating User
const john = new User('john');
// console.log(john.createId()) ❌ ERROR
- Why does
console.log(john.createId())
throw an error?createId()
is a static method, meaning it's called on the class itself (User.createId()
), not on an instance (john.createId()
).- Correct usage:
console.log(User.createId()); // "123"
3. Understanding the Teacher
Subclass
class Teacher extends User {
constructor(username, email) {
super(username);
this.email = email;
}
}
How It Works
extends User
:Teacher
inherits all non-static properties and methods ofUser
.super(username)
: Calls theUser
constructor, ensuringthis.username
is set properly before addingemail
.- Inheritance Chain:
Teacher
does not inheritUser
's static methods (likecreateId()
).
4. Creating a Teacher
Instance
const iphone = new Teacher('iphone', 'i@phone.com');
console.log(iphone.createId()); ❌ ERROR
- Why does
console.log(iphone.createId());
throw an error?createId()
is a static method ofUser
, but static methods do not get inherited by child classes.- Correct usage:
console.log(User.createId()); // "123"
- Fix: If you want
createId()
to be available inTeacher
, redefine it:class Teacher extends User {
static createId() {
return super.createId(); // Calls the static method from User
}
}
console.log(Teacher.createId()); // "123"
📌 Summary of Key Concepts
Feature | Explanation |
---|---|
Static Method (static createId() ) | Belongs to the class itself, not instances. |
Calling Static Methods | Use ClassName.method() , e.g., User.createId() . |
Instance Methods (logMe() ) | Belong to instances, called as object.method() . |
Inheritance (extends User ) | Teacher gets all non-static methods from User . |
Calling Parent Constructor (super(username) ) | Ensures username is set before adding email . |
Static Methods Are Not Inherited | Teacher.createId() does not exist unless explicitly redefined. |
📌 Trivia
- Why are static methods useful?
- Used for utility functions, like generating IDs, formatting data, etc., without needing an instance.
- Can
super
be used in static methods?- Yes,
super.createId()
works inside a static method ofTeacher
becausesuper
refers to the parent class.
- Yes,