
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
When you should not use JavaScript Arrow Functions?
The arrow functions should not be used as an object method because an arrow function does not have its own this. It takes this value of the enclosing lexical scope which is the window object instead of the object itself. This can cause problems as we would now be setting and accessing the window object properties instead of the intended object.
Following is the code showing when should you not use JavaScript arrow functions −
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result { font-size: 20px; font-weight: 500; color: blueviolet; } </style> </head> <body> <h1>When to not use the arrow functions</h1> <br /> <div class="result"></div> <br /> <button class="Btn">CLICK HERE</button> <h3>Click on the above button to call the add() method of object obj</h3> <script> let BtnEle = document.querySelector(".Btn"); let resEle = document.querySelector(".result"); let obj = { a: 22, b: 44, add: () => { return this.a + this.b; }, }; BtnEle.addEventListener("click", (event) => { resEle.innerHTML = `The sum of ${obj.a} and ${obj.b} = ${obj.add()}`; }); </script> </body> </html>
Output
On clicking the ‘CLICK HERE’ button −
- Related Articles
- When should you use sets in Javascript?
- Arrow functions in Javascript
- Lambdas with Arrow Functions in JavaScriptLambdas with Arrow Functions in JavaScript
- How to use arrow functions used as methods in JavaScript?
- Regular functions vs Arrow functions in JavaScript?
- Fat arrow functions in JavaScript
- Concise arrow functions in JavaScript
- When to use anonymous JavaScript functions?
- Higher-Order Arrow Functions in JavaScript.
- Difference between regular functions and arrow functions in JavaScript
- Fat vs concise arrow functions in JavaScript
- Foods You Should not Eat When Treating a UTI
- When should you use 'friend' in C++?
- When Should You Use Remarketing Lists for Search Ads?
- Why should we not use ++, -- operators in JavaScript?

Advertisements