Hi, I am following the session to make some test, but when I execute createFourStopwatchesDemo the safari ended with not any result, it should log more infos like 'StopWatch started'...
Environment : Mac OSX 11.4 Safari Technology Prview 14.2
The Safari console only shows like below When I execute 'createFourStopwatchesDemo()'
Here is my code.
//Example1
//class with public variable and function
class StopWatchWithOneButton {
_startTime = 0;
click(){
if (!this._startTime) {
this.start();
}else{
this.stop();
}
}
start() {
this._startTime = Date.now();
console.log('StopWatch started');
}
stop() {
let duration = Date.now() - this._startTime;
this._startTime = 0;
console.log('StopWatch stopped after %d ms', duration);
}
}
//class with private variable and function
class PrivateStopWatchWithOneButton {
#_startTime = 0;
static #stopWatchCount = 0;
click(){
if (!this.#_startTime) {
this.#start();
}else{
this.#stop();
}
}
#start() {
PrivateStopWatchWithOneButton.#stopWatchCount++;
this.#_startTime = Date.now();
console.log('StopWatch started');
}
#stop() {
PrivateStopWatchWithOneButton.#stopWatchCount--;
let duration = Date.now() - this.#_startTime;
this.#_startTime = 0;
console.log('StopWatch stopped after %d ms', duration);
}
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function demo(){
// console.log(StopWatchWithOneButton._startTime);
var counter = new StopWatchWithOneButton();
counter.click();
console.log('sleep for a while,' + counter._startTime);
await sleep(2000);
console.log('ake up');
counter.click();
//private #_startTime
var priCounter = new PrivateStopWatchWithOneButton();
// console.log('sleep for a while of Private:' + priCounter.#_startTime);//syntax error
priCounter.click();
await sleep(2000);
priCounter.click();
}
//Example2: weak reference
//WeakRef(obj): create weak reference to an object
//use FinalizationRegistry to delete object automaticaly
const allStopWatches = new Map();
var nextAvailableIdentifier = 1;
function removeStopwatch(identifier){
allStopWatches.delete(identifier);
}
const finalizationRegistry = new FinalizationRegistry(removeStopwatch);
function createStopwatch(){
let identifier = nextAvailableIdentifier++;
let stopwatch = new StopWatchWithOneButton();
allStopWatches.set(identifier, new WeakRef(stopwatch));
finalizationRegistry.register(stopwatch, identifier);//the first parameter is an object
return stopwatch;
}
function clickAllStopwatches(){
for(let weakStopwatch in allStopWatches){
//weakStopwatch.deref(): Get the element from the weak reference, if it still exists
weakStopwatch.deref()?.click();
}
}
function createFourStopwatchesDemo(){
for (let i = 0; i < 4; i++) {
let currStop = createStopwatch();
}
clickAllStopwatches();
}
Appreciate for reply in advance ~