Is it posisble to remove the self
variable in this snippet:
class Foobar {
constructor() {
let self = this; // How do I remove self?
let fizz = buzz.doSomething({
aFunction: self.someFun
});
}
someFun() {
}
}
One option could be to define an empty object and then assign the function with the key.
let options = {};
options[aFunction] = this.doSomething;
let fizz = buzz.doSomething(options);
If the options object becomes a list of 10 things wouldn't be better to have them all wrapped by a single pair of braces?
Am I overthinking it?
You can safely use this
in your code snippet. this
only changes it's meaning when you're invoking a function using the function(){}
syntax. In your case you're just creating an object literal, where this
is still the same as self
.
constructor() {
let fizz = buzz.doSomething({
aFunction: this.someFun
});
}
This works perfectly fine. Even made a jsfiddle .
If you want to go into more depth check out this on the MDN.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.