...do not exactly get what above code should proof or disproof...
(function(){
var MyClassA = function(name, checkInInterval)
{
this.name = name;
this.checkInInterval = checkInInterval;
this.TimerID = null;
this.commander = null;
}
MyClassA.prototype.func_1 = function(commander)
{
/* here some code */
this.commander = commander;
this.TimerID = setInterval(this.func_2.bind(this), this.checkInInterval);
}
MyClassA.prototype.func_2 = function()
{
var time = (typeof getTime != "undefined") ? getTime() : new Date().getTime();
console.log(Math.floor(time)
+ ": Hi, my name is " + this.name
+ "; " + this.commander.name
+ " commanded me to check in"
+ " every " + (this.checkInInterval / 1000)
+ " seconds. --- Bye!");
}
var MyClassB = function(name, _object)
{
this.name = name;
this.Obj = _object;
}
MyClassB.prototype.func_1 = function()
{
/* here some code */
this.Obj.func_1(this);
}
var objAlf, objAnn, objBen, objBob;
var onInit = function()
{
objAlf = new MyClassA("Alf",2000);
objAnn = new MyClassA("Ann",5000);
objBen = new MyClassB("Ben",objAlf);
objBob = new MyClassB("Bob",objAnn);
objBen.func_1();
objBob.func_1();
}
setTimeout(onInit, 1000);
})()
Should run in Espruino and provide output in the console like that:
1513144683321: Hi, my name is Alf; Ben commanded me to check in every 2 seconds. --- Bye!
1513144685319: Hi, my name is Alf; Ben commanded me to check in every 2 seconds. --- Bye!
1513144686318: Hi, my name is Ann; Bob commanded me to check in every 5 seconds. --- Bye!
1513144687321: Hi, my name is Alf; Ben commanded me to check in every 2 seconds. --- Bye!
1513144689322: Hi, my name is Alf; Ben commanded me to check in every 2 seconds. --- Bye!
1513144691320: Hi, my name is Alf; Ben commanded me to check in every 2 seconds. --- Bye!
1513144691321: Hi, my name is Ann; Bob commanded me to check in every 5 seconds. --- Bye!
1513144693321: Hi, my name is Alf; Ben commanded me to check in every 2 seconds. --- Bye!
1513144695319: Hi, my name is Alf; Ben commanded me to check in every 2 seconds. --- Bye!
1513144696318: Hi, my name is Ann; Bob commanded me to check in every 5 seconds. --- Bye!
...you can also just open the debugger of (chrome) browser (with 'right-click' Inspect) and paste the code into the console... :)
As the output shows, Alf - objAlf - and Ann - objAnn- know still about their names, their time interval to show up, and who commanded them: Ben and Bob.
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.
...?
...do not exactly get what above code should proof or disproof...
Should run in Espruino and provide output in the console like that:
...you can also just open the debugger of (chrome) browser (with 'right-click' Inspect) and paste the code into the console... :)
As the output shows, Alf - objAlf - and Ann - objAnn- know still about their names, their time interval to show up, and who commanded them: Ben and Bob.