100+ JavaScript

Loop In JS Function localStorage Drag Drop Object get set

Asynchronous JS

Callback Promises

Web API

Web API Intro Cookie Fetch API

JS PDF

Basic Pdf

Toast.js

Basic Toastr

Object getter & setter

In JS, getter & setter allow you to define how to access and update the properties of an object.

Here are key points:
  • get: It has a return value and doesn't take any arguments.
  • set: It takes one argument and doesn't return a value.
  • In JavaScript, the split() method is used to divide a string into an array of substrings, based on a specified separator or delimiter. The original string remains unchanged.

get & set


const Person={
    firstName:"JOhn",
    lastName:"Doe",

    get fullName(){
        return `${this.firstName} ${this.lastName}`;
    },

    set fullName(name){
        let parts=name.split(" ");
        //console.log(parts[0]);
        this.firstName=parts[0];
        this.lastName=parts[1];

    }
}
console.log(Person.fullName);//output:JOhn Doe
Person.fullName="Shah sultan";
console.log(Person.fullName);//output:Shah sultan

🌐Application🈸

Custom Access Control

A user management system that restricts access to sensitive information based on user roles.

    
    const user = {
        _role: 'user',
        _salary: 50000,
    
        get salary() {
            if (this._role === 'admin') {
                return this._salary;
            } else {
                console.error("Access denied: Only admins can view salaries");
                return null;
            }
        }
    };
    
    user._role = 'user';
    console.log(user.salary); // Output: "Access denied: Only admins can view salaries"
    
    user._role = 'admin';
    console.log(user.salary); // Output: 50000
        
    
    

Data Validation

    
    const user = {
        _age: 0,
    
        get age() {
            return this._age;
        },
    
        set age(value) {
            if (value > 0) {
                this._age = value;
            } else {
                console.error("Age must be a positive number");
            }
        }
    };
    
    user.age = 25; // Valid input
    console.log(user.age); // Output: 25
    
    user.age = -5; // Invalid input, triggers validation
    // Output: "Age must be a positive number"