DEV Community

loading...

Discussion on: JavaScript: Watch out for unwanted hoisting!

Collapse
dance2die profile image
Sung M. Kim

I've run into the situation multiple times with old codes.

E.g.)
Some JS files I dealt with are about 1k+ lines long and many people added the same function named get*ById etc.

Collapse
kepta profile image
Kushan Joshi

Oh man thats scary, If someone adds another function getById later on. Any code which was using old getById would start using new getById.

To me this sounds like a bug rather than intentional old way of doing things. What do you think it was ?

Thread Thread
dance2die profile image
Sung M. Kim

My guess is that, some devs used function expressions (e.g. var getById = function()...) instead of function declarations (function getById()...)

Function declarations are hoisted with body but function expressions are not, causing people who don't know how hoisting work declare same code over and over sometimes.