-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Learned about Prototypes and functions prototype with inheritance
- Loading branch information
Showing
3 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
03 - Chai aur Javascript/13 - Prototypes/01_prototype_chain.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
16
03 - Chai aur Javascript/13 - Prototypes/02_function_prototypes.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
36
03 - Chai aur Javascript/13 - Prototypes/03_inheritance.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |