I was reading about lit-html and how it works here.
But i could not understand how they can use the strings
argument as cache-key.
I have read about Map in mdn but i didnt found anything that explains how Map get
and set
works.
My question how the argument strings
can be used as key. Basically how Map stores keys because keys can be anything.
// Store how many times we've seen each literal
let counts = new Map();
// A template tag that updates the count storage and longs the count
let count = (strings) => {
// The strings object is our Map key
let c = (counts.get(strings) || 0) + 1;
counts.set(strings, c);
console.log(`literal ${strings.join('')} seen ${c} time(s)`);
};
// Two functions that evaluate to different tagged literals
let f = (text) => count`abc`;
let g = (text) => count`def`;
f();
f();
g();