I am trying to find a way to run a function of an object literal upon its instantiation in a Web Worker. A closure would be an awesome way to do that but it seems the closure does not have access to Foo
(this
always refers to worker's dedicated global context). Plus, I cannot pass or bind Foo
to the closure because the browser would complain about accessing it before initialization.
Is there any way to modify bar
from init
?
p.s. I'm aware of the option of calling Foo.init()
explicitly, but that's not my intention. I'm trying to avoid that.
const Foo =
{
bar: {},
init: (function (){
//Make some computations and add it to bar
this.bar["key"] = someSpecialValue;
})()
//Doesn't work either:
// })(this)
// })().bind(this)
// })(Foo)
}