Skip to main content

Command Palette

Search for a command to run...

πŸ“˜ JavaScript Objects and Classes

Updated
β€’4 min readβ€’View as Markdown
πŸ“˜ JavaScript Objects and Classes
S

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, and city are properties.

  • greet is a method.

  • this.name ⁣refers to the current object’s name ⁣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.name and this.age refer 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:

  • Dog inherits from Animal using extends.

  • The speak()method is overridden in the Dogclass.


πŸ“Œ 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

FeatureObject LiteralClass-based Object
Syntax{ key: value }class ClassName { constructor() }
Use CaseOne-time or simple objectReusable, structured blueprint
MethodsDefined as propertiesDefined directly in class
ReusabilityLess reusableHighly reusable (via new)
InheritanceNot built-inSupported 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.