🔹JavaScript Tip: bind vs call vs apply These three methods are often confusing, but they all exist for one main reason: controlling the value of this when calling a function. Here’s a simple breakdown 👇 ✅ call() Invokes the function immediately Arguments are passed one by one ✅ apply() Invokes the function immediately Arguments are passed as an array ✅ bind() Does not invoke the function Returns a new function with this (and optional arguments) permanently bound Perfect for callbacks and event handlers Understanding these three makes working with callbacks, event listeners, and frameworks much easier. #JavaScript #WebDevelopment #Frontend #ProgrammingTips #CleanCode
JavaScript: call vs bind vs apply explained
More Relevant Posts
-
📌 call, apply, bind in JavaScript These methods are used to control the value of this inside a function. 🔹 call() Calls a function immediately and sets this. Use case: Borrow a function from another object. 🔹 apply() Same as call, but arguments are passed as an array. Use case: Useful when arguments are already in an array. 🔹 bind() Returns a new function with this permanently bound. Use case: Used in callbacks, event handlers, and async code. Thanks to - aka Anshu Pandey #JavaScript #WebDevelopment #Frontend #JSConcepts #Developers
To view or add a comment, sign in
-
-
Still using JavaScript arrays the same way you always have? Modern array methods can make everyday tasks simpler and your code easier to read. This covers practical tips to help you: • Replace values cleanly • Reset arrays without workarounds • Convert arrays into objects • Sum values efficiently • Find the last occurrence of elements Small improvements like these add up quickly in real projects. Which array method do you rely on most? [Check the Link in the Comment! 🔗] #JavaScript #WebDevelopment #Frontend #CodingTips #ArrayMethods #DevTips #Syncfusion
To view or add a comment, sign in
-
If a component needs 15 props, it's trying to tell you something. Listen. Yep. That component is screaming at you And what it’s saying is: "I'm doing too much." What I would ask in a code review if I see too many props passed to a react component: 1️⃣ Can any of these be grouped? 2️⃣ Is there a missing parent component? 3️⃣ Are we passing data or describing behaviour? 4️⃣ Would this be clearer with composition? #frontend #react #cleancode #javascript
To view or add a comment, sign in
-
-
100 identical requests. 1 expensive DB call! I wrote about request collapsing and how to implement it in NestJS using an interceptor + decorator. https://lnkd.in/dJZK859c #javascript #nestjs #requestsandlimits
To view or add a comment, sign in
-
🤔 How do you decide between using a 𝗦𝗲𝘁 or an 𝗮𝗿𝗿𝗮𝘆 in #JavaScript? Recently I realized that I often reach for arrays by default, even in cases where a Set would be a better fit: when I only care about unique values and fast lookups, not about order. But in many everyday cases with small collections, arrays are still perfectly fine and often more readable. I’m currently trying to be more intentional: if I think “collection of unique things”, I reach for Set; if I need order, duplicates or array utilities, I stick with arrays. How do you handle this in your projects? #JavaScript #TypeScript #CleanCodeSolutions #WebDevelopment #Frontend
To view or add a comment, sign in
-
-
🚀 Stale Closures Why does state inside setTimeout show old values? Functions capture variables at creation time. If state changes later, the timeout still holds old reference. In OTP flows or retry logic, this creates incorrect behavior. Use functional updates or refs when dealing with async callbacks. #ReactJS #JavaScript #AsyncProgramming #Frontend
To view or add a comment, sign in
-
Your API can be fast. Your UI can still feel slow. Why? Unnecessary re-renders No memoization No lazy loading Heavy initial bundle React performance is NOT magic. It’s discipline. #w3school #ReactJS #FrontendPerformance #WebOptimization #MERNStack #ReactHooks #JavaScript
To view or add a comment, sign in
-
-
🔁 Closures in JavaScript A closure is when a function remembers variables from its parent function, even after the parent function has finished executing. function outer() { let count = 0; return function inner() { count++; console.log(count); } } const counter = outer(); counter(); // 1 counter(); // 2 👉 inner() still remembers count 👉 That memory is called a closure If closures ever confused you, save this post 👍 #JavaScript #WebDevelopment #Frontend #CodingSimplified #LearnJS #BhaviDigital
To view or add a comment, sign in
-
-
🤯 This JavaScript snippet will catch you off guard... const obj = { value: 10, double() { return this.value * 2; } }; const { double } = obj; console.log(double()); // What do you think prints? 💭 Most developers say 20. The real answer? NaN. Here's why 👇 When you destructure double from obj, it loses its this binding. Called as a plain function, this no longer points to obj — it points to undefined (strict mode) or the global object (non-strict), where value doesn't exist. So this.value becomes undefined, and undefined * 2 = NaN. ✅ Let me know how will you fix it before the console. The rule to remember: A method is only bound to its object when called AS a method (obj.double()). The moment you detach it, this is gone. This trips up even experienced developers. How many of you got it right? 👇 #JavaScript #WebDev #Frontend #JSInterviewTips #CodingTips #InterviewExperience
To view or add a comment, sign in
-
🚨 Quick JavaScript challenge Looks like a simple object method accessing this.name. Nothing fancy. Should work… right? But the output is not what most people expect. Before running it - Ask yourself: 👉 Does an object create scope? 👉 Where does this really come from? 👉 Arrow vs normal function - who controls it? Guess the output 👇 #JavaScript #Frontend #CodingChallenge
To view or add a comment, sign in
-
More from this author
Explore related topics
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development