Skip to content

Commit

Permalink
Learned about getter and setter methods
Browse files Browse the repository at this point in the history
  • Loading branch information
sohail019 committed Jul 31, 2024
1 parent 4cda264 commit 0831fde
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 0 deletions.
24 changes: 24 additions & 0 deletions 03 - Chai aur Javascript/15 - Getter Setter/01_intro.js
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
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
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
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
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)

0 comments on commit 0831fde

Please sign in to comment.