An interesting turn takes this code - which - in a nutshell:
defines the same structure but with use of constructor and prototype
creates first an obj with id = "objx"
creates second an object with id = "obj"
invokes .meth() on obj
// .bind() not working?
function Obj(id) {
this.id = id;
this.cnt = 0;
}
Obj.prototype.meth = function() {
console.log("-----.meth()-----");
console.log(this);
console.log("id=" + this.id);
if ("obj" == this.id) {
console.log("'this' is 'obj':");
console.log("id="+ this.id + " in invocation # " + (++this.cnt));
if (this.cnt < 3) { var _t = this; setTimeout(_t.meth,1); }
} else {
console.log("'this' is (unexpectedly) NOT 'obj'");
}
console.log("-----/.meth()-----");
};
var objx = new Obj("objx");
var obj = new Obj("obj");
obj.meth.bind(obj);
function onInit() {
obj.meth();
}
The output for the bound this produced with console.log(this) must have somthing to do with the constructor, becauase all - both - instances objx and obj are listed:
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.
An interesting turn takes this code - which - in a nutshell:
invokes
.meth()
onobj
The output for the bound this produced with
console.log(this)
must have somthing to do with the constructor, becauase all - both - instancesobjx
andobj
are listed: