NOTE:Conversation title was EDITED: Read the first three posts - #1..#3 - under the subtitlebind() not working because it was triggered by trying to make require() and .connect() and all kinds of initializers to work correctly after save() and power recycle in a standalone operation using a sequencer as presented in post #6.
In some initialization sequencing I had the construct that simplified to the essentials looks like this:
// .bind() not working?
var obj =
{ id:"obj"
, cnt: 0
, meth:function() {
if ("obj" == this.id) {
console.log("'this' is 'obj':");
console.log("id="+ this.id + " in invocation # " + (++this.cnt));
if (this.cnt < 3) { setTimeout(this.meth,1); }
} else {
console.log("'this' is (unexpectedly) NOT 'obj'");
}
}
};
obj.meth.bind(obj);
function onInit() {
obj.meth();
}
Above code uploaded to Espruino (PICO) and onInit() invoked in the console produces the following output:
|_| http://espruino.com
1v81 Copyright 2015 G.Williams
>echo(0);
=undefined
>onInit();
'this' is 'obj':
id=obj in invocation # 1
=undefined
'this' is (unexpectedly) NOT 'obj'
>
=undefined
First invocation obviously works - because it is a standard invocation - but second - in set Timeout(... in line 10 - fails.
Afaiu fnctn.bind(ctx) binds the ctx object to the function fnctn so that this in function is resolved to ctx context object. As examples shows, Espruino js does not.
Changing line 10 to
if (this.cnt < 3) { var _this = this; setTimeout(_this.meth,1); }
does not help.
Changing onInit() {... to
function onInit() {
var f = obj.meth;
f();
}
shows that .bind() is not working.
|_| http://espruino.com
1v81 Copyright 2015 G.Williams
>echo(0);
=undefined
>onInit();
'this' is (unexpectedly) NOT 'obj'
=undefined
>
I could reason that it would not work with multiple objects created with a constructor and meth had been defined on the prototype, and then bound to one of the created objects, and if multiple times bound, the last would win (...but it is not or neither,... see next post...).
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.
NOTE: Conversation title was EDITED: Read the first three posts - #1..#3 - under the subtitle bind() not working because it was triggered by trying to make
require()
and.connect()
and all kinds of initializers to work correctly after save() and power recycle in a standalone operation using a sequencer as presented in post #6.In some initialization sequencing I had the construct that simplified to the essentials looks like this:
Above code uploaded to Espruino (PICO) and
onInit()
invoked in the console produces the following output:First invocation obviously works - because it is a standard invocation - but second - in
set Timeout(...
in line10
- fails.Afaiu
fnctn.bind(ctx)
binds thectx
object to the functionfnctn
so thatthis
in function is resolved toctx
context object. As examples shows, Espruino js does not.Changing line
10
todoes not help.
Changing
onInit() {...
toshows that
.bind()
is not working.I could reason that it would not work with multiple objects created with a constructor and
meth
had been defined on the prototype, and then bound to one of the created objects, and if multiple times bound, the last would win (...but it is not or neither,... see next post...).