Detailed explanations & code examples are in the JavaScript (.js) file
Click the { } button at the bottom right to view it
Master Objects, Constructors, Classes, and Destructuring
Objects are collections of related properties and methods representing real-world entities
| Operation | Syntax | Example |
|---|---|---|
| Create Object | const obj = {} |
const person = {name: "Umer"} |
| Access Property | object.property |
person.firstName |
| Bracket Notation | object["property"] |
person["lastName"] |
| Add Property | object.newProp = value |
person.city = "Mumbai" |
| Delete Property | delete object.property |
delete person.city |
| Call Method | object.method() |
person.sayHello() |
this refers to the object the method belongs tosayHello: function(){}sayHello: () => {}Blueprints for creating multiple similar objects (pre-ES6 way)
// Constructor Function (starts with capital letter by convention) function Car(make, model, year, color){ this.make = make; // 'this' refers to new object this.model = model; this.year = year; this.color = color; } // Create instances with 'new' keyword const car1 = new Car("Ford", "Mustang", 2024, "red");
| Keyword | Purpose | Explanation |
|---|---|---|
function Car() |
Define Constructor | Creates the blueprint/template |
this |
Reference New Object | Points to the object being created |
new |
Create Instance | Creates a new object from constructor |
{}this to point to that new objectModern, cleaner syntax for creating objects (introduced in ES6/2015)
class Product { // constructor method (runs automatically) constructor(name, price){ this.name = name; this.price = price; } // Methods (no 'function' keyword needed) displayProduct(){ console.log(`Product: ${this.name}`); } } const product1 = new Product("Shirt", 19.99);
| Feature | Constructor Function | Class (ES6) |
|---|---|---|
| Keyword | function |
class |
| Constructor | Function itself | constructor() method |
| Methods | Need function keyword |
No function keyword |
| Inheritance | Complex (prototypes) | Simple (extends) |
| Modern? | ❌ Old way | ✅ Modern standard |
extends - Makes class inherit from parentsuper() - Calls parent class constructorsuper() BEFORE using this in child
MathHelper.getArea(10)product1.displayProduct()Extract values from arrays and objects into separate variables
const colors = ["red", "green", "blue"]; // Extract by position const [first, second, third] = colors; // Skip elements const [color1, , color3] = colors; // Skips "green" // Rest operator const [primary, ...others] = colors;
const person = {firstName: "Umer", age: 23}; // Extract by property name const {firstName, age} = person; // Rename variables const {firstName: fName} = person; // Default values const {country = "India"} = person;
| Type | Syntax | Example |
|---|---|---|
| Array | [element1, element2] |
const [x, y] = array |
| Object | {prop1, prop2} |
const {name, age} = person |
| Skip Array Item | [var1, , var3] |
const [a, , c] = array |
| Rename Object | {prop: newName} |
const {name: n} = person |
| Default Value | {prop = value} |
const {city = "NYC"} = person |
| Function Params | function({prop1, prop2}) |
function({name, age}){} |
| Func Params with default | function({prop1}) |
function({name = 'John Doe', age = 'unknown'}){} |
Loading...