A private method is called to some private parameters. However, they log as “undefined”! The problem area is indicated with a in the code below. Why doesn’t Closure work here? What am I not getting?
(background: I am 3 weeks into learning JS and am making a simple timer as learning exercise)
Thanks in advance, Lenn
function Timer() {
let _startTime , _stopTime, _durationHistory = 0;
let _startTrigger = false;
let _stopTrigger = false;
timerSwitch = function(log, trigger) {
log = Date.now() * 0.001;
trigger = true;
};
this.start = function(){
timerSwitch(_startTime, _startTrigger); //why doesn't closure apply here?
console.log("timer started", _startTime); //the console logs _startTime as Undefined. I'm expecting it to inhert the new Date.now() *0.001
};
this.stop = function(){
timerSwitch(_stopTime,_stopTrigger);
_durationHistory = _durationHistory + (_stopTime - _startTime);
console.log(`timer stopped: ${_stopTime}s | _durationHistory: ${_durationHistory}`);
};
this.reset = function(){
_startTime = 0;
_stopTime = 0;
_durationHistory = 0;
console.log('timer reset: start', _startTime, ' | stop ', _stopTime, '| history ', _durationHistory);
};
this.duration = function(){
console.log(_durationHistory);
};
};
tmr = new Timer();
tmr.start();```