3 ms·
Interesting ways to stretch an objects capabilities but I would likely use a class for stuff like this, in which case the problem would be trivial to solve, in
by stareatgoats 6y ago
Interesting ways to stretch an objects capabilities but I would likely use a class for stuff like this, in which case the problem would be trivial to solve, in a highly legible way.
I'm probably missing something since the author is a renowned author, and I'm just a dabbler. What am I missing though?
- recursive 6y agoI don't think you're missing anything. This is a one liner without any weird stuff. I've never heard of the author FWIW. let lea = { name: "Lea Verou", get id() { return this.explicitId ?? this.name.toLowerCase().replace(/\W+/g, "-"); }, set id(val) { this.explicitId = val; } }; console.log(lea.id); lea.id = 'lv'; console.log(lea.id);
- ivan_ah 6y agoExactly what I was thinking! I've used this computable-valye-but-overridable-with-explicit-value pattern a couple of times, and so long as you stick to a good naming convention it works great.