Write a function `demonstrateThis()` that shows how bind, call, and apply change `this` context.
Problem Statement
Write a function `demonstrateThis()` that shows how bind, call, and apply change `this` context.
Complexity
Time Complexity: -
Space Complexity: -
Hints
š” Hint 1:
call() invokes with a specific this and individual args.
š” Hint 2:
apply() is like call() but takes args as an array.
š” Hint 3:
bind() returns a new function with this permanently bound.
ā
Solution:
```javascript
function demonstrateThis() {
const obj = { name: "Alice" };
function greet(greeting) { return `${greeting}, ${this.name}`; }
return {
call: greet.call(obj, "Hello"),
apply: greet.apply(obj, ["Hi"]),
bind: greet.bind(obj)("Hey")
};
}
```