Skip to content

Commit

Permalink
Learned about Prototypes and functions prototype with inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
sohail019 committed Jul 28, 2024
1 parent f46d513 commit d107a21
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
21 changes: 21 additions & 0 deletions 03 - Chai aur Javascript/13 - Prototypes/01_prototype_chain.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Prototype Chain

// ek animal object create karte hai.
const animal = {
// ek method define karte hai jo console karega
speak(){
console.log("Animal Speaks");
}
};

// ek naya object create karte hai
const dog = Object.create(animal) // iska matlab hai ke ham dog object ko animal ka prototype bana rahe hai, it means ke dog object animal object ke methods aur properties ko inherit kar sakta hai.

// yahan dog object mein naya method add karte hai
dog.bark = function(){
console.log("Dog Barks");
}

// dog ke paas speak method nahi h par wo animal object se inherit karke print karega
dog.speak() // Output: Animal Speaks (from prototype)
dog.bark() // Output: Dog Barks (from own property)
16 changes: 16 additions & 0 deletions 03 - Chai aur Javascript/13 - Prototypes/02_function_prototypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Function Prototypes

function Person(name){
this.name = name
}

// ye method Person constructor se create kiye gae saare objects ke liye available hoga
Person.prototype.greet = function(){
console.log(`Hello my name is ${this.name}`);
}

const person1 = new Person('Sohail')
person1.greet()

const person2 = new Person('Sohail Shaikh')
person2.greet()
36 changes: 36 additions & 0 deletions 03 - Chai aur Javascript/13 - Prototypes/03_inheritance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Inheritance with Prototypes

// constructor function define
function Animal(name){
// har new Animal object jo create hoga uske paas ek name property hogi, jo hamein constructor ke argument se milegi.
this.name = name;
}

// ek method define karte hai jo kisi bhi Animal object ke liye available hoga
Animal.prototype.speak = function(){
console.log(`${this.name} makes noise`);
}

// ek Dog constructor define karte hai
function Dog(name){
// Animal constructor ko Dog ke context mein call kar rahe hai, jisse Dog object bhi name property le sake.
Animal.call(this, name) // Call the parent constructor
}

Dog.prototype = Object.create(Animal.prototype) // Isse ham Dog ko Animal se inherit kar rahe hai, matlab Dog ke objects Animal ke methods ko access kar sakenge

Dog.prototype.constructor = Dog; // Isse ham ensure kar rahe hai ki Dog object ka constructor properly Dog function ko point karey


// ek naya method define karte hai Dog.prototype pe
Dog.prototype.barks = function(){
console.log(`${this.name} barks.`);
}

const myDog = new Dog('Boozo') // Dog class ka ek naya instance(object) bana rahe hai jiska naam Boozo hai

// speak() method call hoga jo Animal se inherit hua
myDog.speak() // Boozo makes noise

// barks() method call hoga jo Dog prototype pe define kiya gaya hai
myDog.barks() // Boozo barks

0 comments on commit d107a21

Please sign in to comment.