JavaScript Decorators & Forwarding (call, apply)
JavaScript provides decorators for modifying functions dynamically and call/apply for function forwarding. These are essential for handling function execution contexts.
🔹 call & apply – Changing Function Context
Both call and apply allow you to explicitly set the this value for a function.
✅ Using call
✔ First argument: The object to use as this.
✔ Remaining arguments: Passed individually.
✅ Using apply
✔ The difference: apply takes arguments as an array.
✅ Using call & apply to Forward a Function
✔ call and apply forward function calls dynamically.
🔹 Function Borrowing
We can borrow methods from one object to another.
✔ The logName method was borrowed using call.
🔹 bind – Permanent Binding
Unlike call and apply, bind returns a new function that permanently binds this.
✔ this is permanently set to user.
🔹 Decorators – Wrapping Functions
A decorator is a function that modifies another function's behavior.
✅ Logging Decorator
✔ The decorator logs the function call without modifying its behavior.
🔹 Throttling & Debouncing (Decorator Examples)
✅ Throttling – Limiting Function Calls
✔ Throttling ensures a function runs at most once per interval.
✅ Debouncing – Delaying Execution Until User Stops
✔ Debouncing ensures a function only runs after a delay (useful for input validation, search boxes).
🎯 Summary
✔ call & apply – Change function this and pass arguments dynamically.
✔ bind – Permanently set this for a function.
✔ Decorators – Wrap functions to modify behavior (e.g., logging, throttling, debouncing).
✔ Throttling – Limits function calls.
✔ Debouncing – Delays execution until activity stops.
🚀 Decorators and function forwarding make JavaScript more powerful! Let me know if you need more examples. 😊

