Advanced Functions: =================== Arrow Functions: ================ -> introduced in ES6 (ECMAScript 2015) -> Arrow function is the compact way to define the function. -> Arrow function possible to define without "function" keyword. const orders = [ {id : 1,status :"pending"}, {id : 2,status : "delivered"}, {id : 3,status : "shipped"}, {id : 4,status : "delivered"} ]; // without arrow functions const deliveredOrders = orders.filter(function(order){ return order.status === "delivered"; // anonymous function ==> Nameless functions }) console.log(deliveredOrders); // with arrow function console.log("The Products which are already delivered:"); const delivered = orders.filter(order => order.status === "delivered"); console.log(delivered); ================================================== Function Invocations: ===================== ==> The function can call in different ways: 1) Direct Call 2) as an Object Properties 3) New Keyword 4) using call(), apply() and bind() 5) Automatic way // Online shopping cart function calculateTotal(cartItems){ let total = 0 cartItems.forEach(item => { total += item.price * item.quantity; }); return total; } let cart = [ {name:"Laptop",price:50000,quantity:1}, {name:"Moouse",price:1100,quantity:2}, {name:"Keyboard",price:2500,quantity:1} ]; // Direct Call to the function ==> Traditional way of calling the function const totalPriceOfTheCart = calculateTotal(cart); console.log("The Total Cost of Products from the cart = ",totalPriceOfTheCart); // Function call as a object property const shoppingCart = { cartItems : cart, calculateTotal(){ return calculateTotal(this.cartItems); } }; console.log(shoppingCart.calculateTotal()); // using call() const otherCart = [ {name:"mobilePhone",price:25000,quantity:1}, {name:"charger",price:1500,quantity:1} ]; const totalOtherCart = calculateTotal.call(null,otherCart); console.log(totalOtherCart); ========================================== apply(): ======= function calculateAge(currentYear){ return currentYear - this.birthYear; } let person = { name : "Karthik", birthYear:1993 }; let anotherPerson = { name : "Kishore", birthYear : 1996 }; let kathikAge = calculateAge.apply(person,[2025]); console.log("The Age of Karthik = ",kathikAge); let kishoreAge = calculateAge.apply(anotherPerson,[2025]); console.log("The Age of Kishore = ",kishoreAge) ===================================================== bind(): ====== let student1 = { name : "Rajesh", grade : "A", selfIntro:function(){ console.log(this.name+" My Grade is = "+this.grade); } }; let student2 = { name : "Keerthana", grade : "B", } let result = student1.selfIntro.bind(student2); result()