-
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 getter and setter methods
- Loading branch information
Showing
5 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
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 @@ | ||
const person = { | ||
firstName: 'Sohail', | ||
lastName: 'Shaikh', | ||
|
||
// Getter | ||
get fullName(){ | ||
// return value | ||
return `${this.firstName} ${this.lastName}` // first name aur last name ko combine karke full name return karega | ||
}, | ||
|
||
// Setter | ||
set fullName(name){ | ||
// set value | ||
const parts = name.split(' ') // full name ko split karke first name aur last name properties ko update kar sakenge | ||
this.firstName = parts[0] | ||
this.lastName = parts[1] | ||
} | ||
} | ||
|
||
console.log(person.fullName); // Sohail Shaikh | ||
|
||
person.fullName = 'Salman Khan' | ||
console.log(person.firstName); // Salman | ||
console.log(person.lastName); // Khan |
24 changes: 24 additions & 0 deletions
24
03 - Chai aur Javascript/15 - Getter Setter/02_getter_setter_with_classes.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 @@ | ||
// Getters and Setters with Classes | ||
|
||
class Rectangle { | ||
constructor(width, height) { | ||
this.width = width; | ||
this.height = height; | ||
} | ||
|
||
// Getter | ||
get area() { | ||
return this.width * this.height; // rectangle ka area return karega | ||
} | ||
|
||
// Setter for area (assuming a fixed width) | ||
set area(newArea) { | ||
this.height = newArea / this.width; // height ko is tarah adjust karega ke given area maintain ho | ||
} | ||
} | ||
|
||
const rect = new Rectangle(10, 20) | ||
console.log(`Area of Rectangle is ${rect.area}`); | ||
|
||
rect.area = 300 | ||
console.log(`Width of Rectangle is ${rect.height}`); // height becomes 30 |
32 changes: 32 additions & 0 deletions
32
03 - Chai aur Javascript/15 - Getter Setter/03_data_validation_in_setter.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,32 @@ | ||
// Data Validation | ||
|
||
class BankAccount{ | ||
constructor(balance){ | ||
this.balance = balance | ||
} | ||
|
||
// getter for balance | ||
get balance(){ | ||
return this.balance | ||
} | ||
|
||
// Setter balance with validation | ||
|
||
set balance(amount){ | ||
if(amount < 0){ | ||
console.log('Amount cannot be negative'); | ||
} else{ | ||
this._balance = amount | ||
} | ||
} | ||
} | ||
|
||
|
||
const account = new BankAccount(1000); | ||
console.log(account._balance); // 1000 | ||
|
||
account.balance = 500 // updates balance | ||
console.log(account._balance); // 500 | ||
|
||
account.balance = -300 // Attempt to set invalid balance | ||
// Output: // Amount cannot be negative |
33 changes: 33 additions & 0 deletions
33
03 - Chai aur Javascript/15 - Getter Setter/04_private_properties_using_symbol.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,33 @@ | ||
// Private Properties using Symbol | ||
|
||
const _salary = Symbol('salary') | ||
|
||
class Employee{ | ||
constructor(name, salary){ | ||
this.name = name | ||
this[_salary] = salary | ||
} | ||
|
||
// Getter for salary | ||
get salary(){ | ||
return this[_salary] | ||
} | ||
|
||
// Setter for salary | ||
set salary(amount){ | ||
if (amount > 0) { | ||
this[_salary] = amount | ||
} else{ | ||
console.log(`Salary must be positive`); | ||
} | ||
} | ||
} | ||
|
||
const emp = new Employee('Sohail', 10000) | ||
console.log(emp.salary); // Output: 10000 | ||
|
||
emp.salary = 6000 | ||
console.log(emp.salary); // Output: 6000 | ||
|
||
emp.salary = -1000; // Attempt to set invalid salary | ||
// Output: Salary must be positive |
30 changes: 30 additions & 0 deletions
30
03 - Chai aur Javascript/15 - Getter Setter/05_lazy_evaluation_with_getter.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,30 @@ | ||
// Lazy Evaluation with Getter | ||
|
||
class ExpensiveCalculation{ | ||
constructor(){ | ||
this._result = null | ||
} | ||
|
||
// Getter for result with lazy evaluation | ||
get result(){ | ||
if(this._result === null){ | ||
console.log(`Calculating result......`); | ||
this._result = this.performExpensiveCalcualtion() | ||
} | ||
return this._result | ||
} | ||
|
||
performExpensiveCalcualtion(){ | ||
// Simulating a time consuming calculation | ||
let sum = 0 | ||
for(let i = 0; i <= 1e6; i++){ | ||
sum += i | ||
} | ||
return sum | ||
} | ||
} | ||
|
||
const calc = new ExpensiveCalculation() | ||
console.log(calc.result) // Output: Calculating result...... 500000500000 | ||
|
||
console.log((calc.result)); // 500000500000 (without recalculating) |