DEV Community

loading...

Discussion on: 5 easy wins for cleaner Javascript code 🧹

Collapse
mikolaj6r profile image
Mikołaj Romanowski

Actually, your 3rd point is wrong. I dont know how Typescript would behave, but as for JS what You do in second example is assigning variable user to new object. And You loose properties set by User constructor

Collapse
mlevkov profile image
Mikhail Levkovsky Author

You’re absolutely right! I forgot to type it! Will update the gist shortly. Thanks for the catch

Collapse
solarliner profile image
🇨🇵️ Nathan Graule

It's not only about Typescript typing; TS is right about changing the type of your variable because you're assigning a completely new value to it. Doesn't matter if it's an User object, or a string or a number, she the assignment, user is an object with the specified values in it. The User prototype is lost.