diff --git a/03 - Chai aur Javascript/13 - Prototypes/04_prototype_methods_and_properties.js b/03 - Chai aur Javascript/13 - Prototypes/04_prototype_methods_and_properties.js new file mode 100644 index 0000000..c5f9f80 --- /dev/null +++ b/03 - Chai aur Javascript/13 - Prototypes/04_prototype_methods_and_properties.js @@ -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 \ No newline at end of file diff --git a/03 - Chai aur Javascript/13 - Prototypes/05_modifying_prototypes.js b/03 - Chai aur Javascript/13 - Prototypes/05_modifying_prototypes.js new file mode 100644 index 0000000..b7d2bd8 --- /dev/null +++ b/03 - Chai aur Javascript/13 - Prototypes/05_modifying_prototypes.js @@ -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 \ No newline at end of file diff --git a/03 - Chai aur Javascript/13 - Prototypes/06_setPrototypeOf.js b/03 - Chai aur Javascript/13 - Prototypes/06_setPrototypeOf.js new file mode 100644 index 0000000..1f7898a --- /dev/null +++ b/03 - Chai aur Javascript/13 - Prototypes/06_setPrototypeOf.js @@ -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 diff --git a/03 - Chai aur Javascript/13 - Prototypes/07_getPrototypeOf.js b/03 - Chai aur Javascript/13 - Prototypes/07_getPrototypeOf.js new file mode 100644 index 0000000..2c8caf0 --- /dev/null +++ b/03 - Chai aur Javascript/13 - Prototypes/07_getPrototypeOf.js @@ -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]} \ No newline at end of file diff --git a/03 - Chai aur Javascript/13 - Prototypes/08_hasOwnProperty.js b/03 - Chai aur Javascript/13 - Prototypes/08_hasOwnProperty.js new file mode 100644 index 0000000..8c4842e --- /dev/null +++ b/03 - Chai aur Javascript/13 - Prototypes/08_hasOwnProperty.js @@ -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) \ No newline at end of file diff --git a/03 - Chai aur Javascript/13 - Prototypes/09_object_composition.js b/03 - Chai aur Javascript/13 - Prototypes/09_object_composition.js new file mode 100644 index 0000000..f9ddeab --- /dev/null +++ b/03 - Chai aur Javascript/13 - Prototypes/09_object_composition.js @@ -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 \ No newline at end of file