Skip to content

Latest commit

 

History

History
18 lines (16 loc) · 400 Bytes

map.md

File metadata and controls

18 lines (16 loc) · 400 Bytes

Extending Map

class ExtendedMap extends Map {
	getOrDefault(key, value) {
  	if (cache.has(key)) {
    	return cache.get(key)
    }
    if (value === null || value === undefined) return null
    return cache.set(key, value) && cache.get(key)
  }
}

const cache = new ExtendedMap()
console.log(cache.get('item'))
console.log(cache.getOrDefault('item', 1))
console.log(cache.get('item'))