Skip to content

Commit

Permalink
Learned about prototype methods, modify prototype, some methods and o…
Browse files Browse the repository at this point in the history
…bject composition
  • Loading branch information
sohail019 committed Jul 29, 2024
1 parent e0c1dc6 commit 4e941c3
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 0 deletions.
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
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 03 - Chai aur Javascript/13 - Prototypes/06_setPrototypeOf.js
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 03 - Chai aur Javascript/13 - Prototypes/07_getPrototypeOf.js
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 03 - Chai aur Javascript/13 - Prototypes/08_hasOwnProperty.js
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 03 - Chai aur Javascript/13 - Prototypes/09_object_composition.js
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

0 comments on commit 4e941c3

Please sign in to comment.