Hacker News new | ask | show | jobs
by MrJohz 1231 days ago
In that case, why not just create an object? With a constructor function, that could look something like

    (name: string, age: number) => ({ name, age: age * 2 })
Or even just without the constructor, if the data is so simple:

    { name: "Bill", age: 62 }
I'd have thought that would be the quickest option, because then you're not messing around with prototypes at all.

Also, what do you mean by the accessor shorthand? I don't know what you mean off the top of my head, and searching for it has just brought me back to this post!

1 comments

Using a class has some JIT optimization benefits if you always construct a value of the same shape.

Accessors: https://devblogs.microsoft.com/typescript/announcing-typescr...

That's true of objects too, as long as they have the same shape (which Typescript naturally helps with!). As long as the optimising compiler can see that your objects all have the same shape, and that you're always using the same properties, you're golden.

I hadn't noticed the accessors thing before, but it looks like that's an ECMAScript/TC39 thing, I guess to support the decorator implementation. Thanks for the link!