From d107a211c4f0f6d657eb33b5280917a0811544de Mon Sep 17 00:00:00 2001 From: sohail019 Date: Sun, 28 Jul 2024 23:06:41 +0530 Subject: [PATCH] Learned about Prototypes and functions prototype with inheritance --- .../13 - Prototypes/01_prototype_chain.js | 21 +++++++++++ .../13 - Prototypes/02_function_prototypes.js | 16 +++++++++ .../13 - Prototypes/03_inheritance.js | 36 +++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 03 - Chai aur Javascript/13 - Prototypes/01_prototype_chain.js create mode 100644 03 - Chai aur Javascript/13 - Prototypes/02_function_prototypes.js create mode 100644 03 - Chai aur Javascript/13 - Prototypes/03_inheritance.js diff --git a/03 - Chai aur Javascript/13 - Prototypes/01_prototype_chain.js b/03 - Chai aur Javascript/13 - Prototypes/01_prototype_chain.js new file mode 100644 index 0000000..5164687 --- /dev/null +++ b/03 - Chai aur Javascript/13 - Prototypes/01_prototype_chain.js @@ -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) \ No newline at end of file diff --git a/03 - Chai aur Javascript/13 - Prototypes/02_function_prototypes.js b/03 - Chai aur Javascript/13 - Prototypes/02_function_prototypes.js new file mode 100644 index 0000000..1ba2256 --- /dev/null +++ b/03 - Chai aur Javascript/13 - Prototypes/02_function_prototypes.js @@ -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() \ No newline at end of file diff --git a/03 - Chai aur Javascript/13 - Prototypes/03_inheritance.js b/03 - Chai aur Javascript/13 - Prototypes/03_inheritance.js new file mode 100644 index 0000000..dbcf205 --- /dev/null +++ b/03 - Chai aur Javascript/13 - Prototypes/03_inheritance.js @@ -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 \ No newline at end of file