-
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 prototype methods, modify prototype, some methods and o…
…bject composition
- Loading branch information
Showing
6 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
03 - Chai aur Javascript/13 - Prototypes/04_prototype_methods_and_properties.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,24 @@ | ||
// Prototype Methods and Properties | ||
|
||
function Car(brand, model){ | ||
this.brand = brand | ||
this.model = model | ||
} | ||
|
||
// Create Prototype Method | ||
Car.prototype.start = function(){ | ||
console.log(`${this.brand} ${this.model} starts.`); | ||
} | ||
|
||
// Create Prototype Property | ||
Car.prototype.year = 2024 | ||
|
||
|
||
// Object | ||
const myCar = new Car("Mahindra", "Scorpio-N") | ||
|
||
// Call method | ||
myCar.start() // Output: Mahindra Scorpio-N starts | ||
|
||
// Call property | ||
console.log(myCar.year) // Output : 2024 |
21 changes: 21 additions & 0 deletions
21
03 - Chai aur Javascript/13 - Prototypes/05_modifying_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,21 @@ | ||
// Modifying Prototypes | ||
|
||
function Vehicle(type){ | ||
this.type = type | ||
} | ||
|
||
// Prototype method | ||
Vehicle.prototype.describe = function(){ | ||
console.log(`This is a ${this.type}`); | ||
} | ||
|
||
// Object | ||
const bike = new Vehicle('bike') | ||
bike.describe() // This is a bike | ||
|
||
|
||
Vehicle.prototype.describe = function(){ | ||
console.log(`The type of the Vehicle is ${this.type}`); | ||
} | ||
|
||
bike.describe() // The type of the Vehicle is bike |
23 changes: 23 additions & 0 deletions
23
03 - Chai aur Javascript/13 - Prototypes/06_setPrototypeOf.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,23 @@ | ||
// setPrototypeOf | ||
|
||
const animal = { | ||
speak(){ | ||
console.log("Animal Speaks."); | ||
} | ||
} | ||
|
||
const dog = { | ||
bark() { | ||
console.log("Dog barks."); | ||
} | ||
} | ||
|
||
Object.setPrototypeOf(dog, animal) | ||
|
||
dog.speak() // Output: Animal speaks (inherited from prototype) | ||
|
||
dog.bark() // Output: Dog barks | ||
|
||
animal.speak() // Output : Animal speaks | ||
|
||
animal.bark() // Error: animal.bark is not a function |
11 changes: 11 additions & 0 deletions
11
03 - Chai aur Javascript/13 - Prototypes/07_getPrototypeOf.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,11 @@ | ||
// Object.getPrototypeOf.js | ||
|
||
const animal = { | ||
speak(){ | ||
console.log("Animal speaks"); | ||
} | ||
} | ||
|
||
const dog = Object.create(animal) | ||
|
||
console.log(Object.getPrototypeOf(dog)); // Output {speak : [Function speak]} |
20 changes: 20 additions & 0 deletions
20
03 - Chai aur Javascript/13 - Prototypes/08_hasOwnProperty.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,20 @@ | ||
// Object.hasOwnProperty() | ||
|
||
const animal = { | ||
speak(){ | ||
console.log("Animal speaks"); | ||
} | ||
} | ||
|
||
const dog = Object.create(animal) | ||
|
||
|
||
dog.bark = function(){ | ||
console.log("Dog barks"); | ||
} | ||
|
||
dog.bark() | ||
dog.speak() | ||
|
||
console.log(dog.hasOwnProperty('bark')); // true (own property) | ||
console.log(dog.hasOwnProperty('speak')); // false (inherited property) |
20 changes: 20 additions & 0 deletions
20
03 - Chai aur Javascript/13 - Prototypes/09_object_composition.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,20 @@ | ||
// Object Composition | ||
|
||
const vehicle = { | ||
start(){ | ||
console.log("Vehicle started"); | ||
} | ||
} | ||
|
||
// Object.assign({}, vehicle, {drive(){}}) | ||
// Pehla argument {} ek empty object hai, jis mein saari properties copy hogi. | ||
// Dusra argument vehicle hai, jisse start method copy hoga | ||
// Teesra argument ek naya object hai jisme drive method define kiya jaega. | ||
const car = Object.assign({}, vehicle, { | ||
drive(){ | ||
console.log("Car is Driving"); | ||
} | ||
}) | ||
|
||
car.start() // Output: Vehicle started | ||
car.drive() // Output: Car is driving |