OOPs part-02: ============= Static Method: ============== Instance Method in a class can execute differently from object to object. whereas, the static method cannot execute differently from object to object. That means, the static method execution can be same for all objects of that class. class UserValidator{ static isValidEmail(email){ const emailRegex = /^[^\s@]+\.[^\s@]+$/; return emailRegex.test(email); } static isValidPassword(password){ const passwordRegex = /^(?=.*[0-9])(?=.*[a-zA-Z]).{8,}$/; return passwordRegex.test(password); } } const email = 'test.$ravi.@gmail.com'; const password = 'password123'; console.log(UserValidator.isValidEmail(email)); console.log(UserValidator.isValidPassword(password)); ================================================================ OOPs Principles: ================ 1) Encapsulation 2) Polymorphism 3) Inheritance 4) Abstraction 1) Encapsulation ================ class Product{ constructor(id,name,price){ this.id = id; this.name = name; this.price = price; } } class Cart{ #items = []; // private data #total = 0; addProduct(product,quantity = 1){ const existingItem = this.#items.find(item => item.product.id === product.id); if(existingItem){ existingItem.quantity += quantity; } else{ this.#items.push({product,quantity}); } } removeProduct(productId){ this.#items = this.#items.filter(item => item.product.id !== productId); this.#updateTotal(); } #updateTotal(){ this.#total = this.#items.reduce((acc,item) => acc + item.product.price * item.quantity,0); } getTotal(){ return this.#total; } getItems(){ return this.#items.map(item => {product:item.product;quantity:item.quantity;}); } } class UI{ static displayCart(cart){ console.log("Cart Contents :"); cart.getItems().forEach(item => { console.log(`Product:${item.product.name},Quantity:${item.quantity},SubTotal:${item.product.price * item.quantity}`); }); console.log(`Total:$${Cart.getTotal()}`); } } let p1 = new Product(1,"Laptop",50000); let p2 = new Product(2,"Mobile",25000); const cart = new Cart(); cart.addProduct(p1,1); cart.addProduct(p2,2); UI.displayCart(cart);