0% found this document useful (0 votes)
9 views4 pages

JS Constructors Notes

This document provides comprehensive notes on JavaScript constructors, explaining their purpose, syntax, and usage with real-life examples. It covers the 'new' keyword, method addition within constructors, ES6 class syntax, inheritance, and common mistakes. Additionally, it includes a quick revision section summarizing key points about constructors and their functionalities.

Uploaded by

hey327728
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

JS Constructors Notes

This document provides comprehensive notes on JavaScript constructors, explaining their purpose, syntax, and usage with real-life examples. It covers the 'new' keyword, method addition within constructors, ES6 class syntax, inheritance, and common mistakes. Additionally, it includes a quick revision section summarizing key points about constructors and their functionalities.

Uploaded by

hey327728
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

■ JavaScript Constructors

Complete Notes — Hinglish mein | BCA / Internship Ready

1. Constructor Kya Hota Hai?


Constructor ek special function hota hai jo naye objects banane ke liye use hota hai. Jab hum new keyword
use karte hain, to constructor call hota hai aur ek fresh object return karta hai.

Real-Life Example:
Socho ek Car factory hai — same blueprint se alag-alag cars banti hain. Constructor = Blueprint, Object = Car

Basic Syntax:
function Person(name, age) {
[Link] = name; // property assign
[Link] = age;
}

// 'new' keyword se object banao


const p1 = new Person('Afzal', 20);
const p2 = new Person('Rahul', 22);

[Link]([Link]); // Afzal
[Link]([Link]); // 22

2. 'new' Keyword Kya Karta Hai? (Step-by-Step)


Step Kya Hota Hai (Internally)

1 Ek naaya empty object {} create


hota hai

2 this us naaye object ko point


karta hai

3 Constructor function execute


hota hai

4 Naaya object automatically


return ho jaata hai

3. Constructor mein Methods Add Karna


Constructor ke andar methods bhi define kar sakte ho, lekin best practice hai ki methods prototype pe add
karo — memory save hoti hai.
// ■ Andar method — har object ki apni copy banti hai (memory waste)
function Car(brand) {
[Link] = brand;
[Link] = function() { // BAD
[Link]([Link] + ': Beep!');
};
}

// ■ Prototype pe method — ek hi copy, sab share karte hain (GOOD)


function Car(brand) {
[Link] = brand;
}
[Link] = function() {
[Link]([Link] + ': Beep!');
};

const c1 = new Car('Honda');


[Link](); // Honda: Beep!

4. ES6 Class Syntax (Modern Way)


class keyword ES6 mein aaya — ye constructor ka hi syntactic sugar hai. Andar se same kaam karta hai,
bas likhna easy ho gaya. ■
class Student {
constructor(name, roll) { // same as old constructor fn
[Link] = name;
[Link] = roll;
}

greet() { // automatically prototype pe jaata hai


[Link](`Hi, I am ${[Link]}, Roll: ${[Link]}`);
}
}

const s1 = new Student('Afzal', 42);


[Link](); // Hi, I am Afzal, Roll: 42

5. Inheritance (Ek Constructor se Doosra Banao)


Inheritance matlab ek class ki properties/methods doosri class le le. extends + super() use hota hai.
class Animal {
constructor(name) {
[Link] = name;
}
speak() {
[Link](`${[Link]} makes a sound.`);
}
}

class Dog extends Animal { // Dog inherits Animal


constructor(name, breed) {
super(name); // Animal ka constructor call karo
[Link] = breed;
}
bark() {
[Link](`${[Link]} (${[Link]}): Woof!`);
}
}
const d = new Dog('Bruno', 'Labrador');
[Link](); // Bruno makes a sound.
[Link](); // Bruno (Labrador): Woof!

6. Old vs New — Quick Comparison


Feature Old (function) New (class ES6)

Define karna function Person(){} class Person{}

Constructor function itself constructor(){}

Method add [Link] method() inside class

Inheritance call() / [Link]() extends + super()

Hoisting Haan (hoisted) Nahi (TDZ)

7. instanceof Operator
instanceof se check karte hain ki koi object kisi particular constructor/class ka instance hai ya nahi.
class Bike {}
class Car {}

const myBike = new Bike();

[Link](myBike instanceof Bike); // true


[Link](myBike instanceof Car); // false

8. Common Mistakes ■■
• new bhool gaye
■ const p = Person('Ali'); // ■ this = window/undefined
■ const p = new Person('Ali'); // ■

• return object kiya andar se


■ function F(){ return {x:1}; } // new ignore ho jaata hai
■ Avoid karo — constructor mein return mat karo

• Arrow function as constructor


■ const F = () => {}; new F(); // ■ TypeError
■ Arrow fns constructor nahi ban sakti — regular fn use karo

■ Quick Revision — Yaad Rakho!


✦ Constructor = Object banane ka blueprint
✦ 'new' keyword se object create hota hai
✦ this = naya bana hua object
✦ Methods prototype pe daalo — memory save hoti hai
✦ ES6 class = same kaam, better syntax
✦ extends + super() = inheritance
✦ Arrow function constructor nahi ban sakti
✦ instanceof se type check karo

All the best Afzal! ■ — JS Constructors Done ■

You might also like