π JavaScript Objects and Classes

Full Stack Developer Intern at DevSync
Presented by Swapnil Meshram within mentorship Devsync
π Started learning JavaScript with the guidance of Devsync, focusing on concepts of objects and classes with simple hands-on practice.
β What are Objects in JavaScript?
In JavaScript, an object is a collection of key-value pairs. Itβs used to group related data and functionality together.
An object can contain:
Properties (data)
Methods (functions)
π 1. Creating an Object
πΉ Using Object Literal
let person = {
name: "Alice",
age: 25,
city: "Mumbai",
greet: function () {
console.log("Hello, my name is " + this.name);
}
};
π Explanation:
name,age, andcityare properties.greetis a method.this.nameβ£refers to the current objectβsnameβ£property.
πΉ Accessing Object Properties
console.log(person.name); // Output: Alice
console.log(person["city"]); // Output: Mumbai
πΉ Calling a Method
person.greet(); // Output: Hello, my name is Alice
π 2. Adding & Modifying Object Properties
person.country = "India"; // Add new property
person.age = 26; // Modify property
πΉ Deleting a Property
delete person.city;
π 3. Nested Objects
let student = {
name: "Rahul",
marks: {
math: 90,
science: 85
}
};
console.log(student.marks.math); // Output: 90
π 4. for...in Loop to Iterate an Object
for (let key in person) {
console.log(key + ": " + person[key]);
}
β What are classes in JavaScript?
A class is a blueprint for creating objects with predefined properties and methods. It was introduced in ES6 (ECMAScript 2015).
Classes make your code more organised and reusable, especially when creating multiple similar objects.
π 5. Declaring a Class
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
console.log("Hi, Iβm " + this.name);
}
}
π Explanation:
constructor()is a special method that initialises object properties.this.nameandthis.agerefer to the properties of the object created.greet()is a method inside the class.
π 6. Creating Objects from a Class
let user1 = new Person("Amit", 30);
let user2 = new Person("Priya", 28);
user1.greet(); // Output: Hi, Iβm Amit
user2.greet(); // Output: Hi, Iβm Priya
π 7. Adding Methods Outside Constructor
You can define multiple methods in a class:
class Car {
constructor(brand) {
this.brand = brand;
}
start() {
console.log(this.brand + " is starting...");
}
stop() {
console.log(this.brand + " is stopping...");
}
}
let myCar = new Car("Toyota");
myCar.start(); // Output: Toyota is starting...
π 8. Inheritance with Classes
A class can inherit properties and methods from another class using extends.
class Animal {
constructor(name) {
this.name = name;
}
speak() {
console.log(this.name + " makes a sound.");
}
}
class Dog extends Animal {
speak() {
console.log(this.name + " barks.");
}
}
let dog = new Dog("Tommy");
dog.speak(); // Output: Tommy barks.
π Explanation:
Doginherits fromAnimalusingextends.The
speak()method is overridden in theDogclass.
π 9. Getters and Setters in Classes
πΉ Getters
Used to access object properties like methods.
class Circle {
constructor(radius) {
this._radius = radius;
}
get area() {
return Math.PI * this._radius ** 2;
}
}
let c = new Circle(5);
console.log(c.area); // Output: 78.54 (approx)
πΉ Setters
Used to change private-like properties.
class Circle {
set radius(r) {
this._radius = r;
}
get radius() {
return this._radius;
}
}
let c = new Circle();
c.radius = 10;
console.log(c.radius); // Output: 10
π 10. Static Methods
A static method belongs to the class itself, not any instance.
class MathUtils {
static square(x) {
return x * x;
}
}
console.log(MathUtils.square(5)); // Output: 25
β Summary: Objects vs Classes
| Feature | Object Literal | Class-based Object |
| Syntax | { key: value } | class ClassName { constructor() } |
| Use Case | One-time or simple object | Reusable, structured blueprint |
| Methods | Defined as properties | Defined directly in class |
| Reusability | Less reusable | Highly reusable (via new) |
| Inheritance | Not built-in | Supported with extends |
π§ Final Tips
Use objects when you just need to group related data.
Use classes when creating multiple similar objects or for OOP (Object-Orientated Programming).
Practice by creating classes for products, students, vehicles, or employees.