4 ms·
That proposal is ugly. I'd rather just use: class Translation{ static get translations(){ return {blah: things}; } sta
by drenvuk 5y ago
That proposal is ugly. I'd rather just use:
class Translation{
static get translations(){
return {blah: things};
}
static englishWords(){ return Object.keys(this.translations);}
}
Since it's all static can't browsers optimize what's already there in this case? I feel like I'm missing something.
- inbx0 5y agoThere's an observable difference with your getter based approach and the "normal property values" approach in the article: const t = new Translation // property value that // is initialized once t.translations === t.translations // getters which return // new objects t.translations !== t.translations so at the very least the browser can't optimize that behavior away, it needs to create new objects. Besides, ensuring that say, call to `Object.keys` is side-effect free is challenging.
- drenvuk 5y agoAre you not talking about a class instance? shouldn't all of this initialization happen inside of the constructor? If we're talking only about static properties it seems more reasonable to me to freeze them upon definition or first use.
- inbx0 5y agoThe const t = new Translation was indeed a mistake in my example, since we are talking about static properties and not instance properties. But the point still stands, just remove "new" from that example and the same applies, the getter-based approach creates new objects each time. > it seems more reasonable to me to freeze them upon definition or first use I agree, and that's why I'd default to static translations = {blah: things} and not static get translations() { return {blah: things} } the latter does not "freeze" them