You need to use bla.init.bind(bla) or function(){bla.init()}, not just bla.init.
It's one of the 'gotchas' of JavaScript, and it's the same thing that happens for setInterval, setTimeout, or any callback.
Basically, when you call a function, like bla.init(), the this variable gets set up to bla. However, if you just pass bla.init, you're basically doing this:
var x = bla.init;
x();
In that case, x is just a function, that has no association with bla at all - so when you call it, this isn't set.
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
You need to use
bla.init.bind(bla)
orfunction(){bla.init()}
, not justbla.init
.It's one of the 'gotchas' of JavaScript, and it's the same thing that happens for
setInterval
,setTimeout
, or any callback.Basically, when you call a function, like
bla.init()
, thethis
variable gets set up tobla
. However, if you just passbla.init
, you're basically doing this:In that case,
x
is just a function, that has no association withbla
at all - so when you call it,this
isn't set.