I was reading about Arrow Functions and found out that they can't have their context changed.
I was creating a module that receives a function and then changes its context. But since the user could be inputing an arrow function I couldn't make it happen.
So I was wondering if, since it's not possible to change an arrow function context, I could copy its content and create a new function that does the same, but now with a controlled context.
Any ideas how it could be achieved?
An example is something like this:
class Foo {
constructor(name) {
this.name = name;
}
sayMyName() {
console.log(this.name);
return this.name;
}
}
class Scope {
constructor(reqId) {
this.foo = new Foo('Hi!');
this.reqId = reqId;
}
do(callback) {
const func = callback.bind(this, this);
func();
}
}
class Controller {
constructor() {
this.foo = new Foo('Hello!');
}
unscoped(req, res, next) {
var a = 1;
res.json({
success: this.foo.sayMyName()
});
}
scoped(req, res, next) {
req.scope.do((ctx) => {
var a = 1;
res.json({
success: this.foo.sayMyName()
});
});
}
}
I want this.foo.sayMyName()
to return 'hi' in Controller.scoped
and 'hello' in Controller.unscoped