r/ProgrammerHumor • • 18d ago

Meme iFixedThePreviousPostBasedOnBestComments

Post image
708 Upvotes

77 comments sorted by

View all comments

Show parent comment

20

u/FrostSalamander 18d ago

Wtf on item 4, thats like the inverse of all other languages

4

u/the_horse_gamer 18d ago edited 18d ago

javascript kinda has something similar

imagine you have an object a such that a.x is a (non-arrow, non-bound) function

if you call a.x(), the value of this inside the function is a

but if you store a.x in a variable and then call it, the value of this is undefined (in strict mode). you can then even do b.y = a.x, and calling b.y() has this equal to b

one way to solve this is to do a.x.bind(a) and now no matter what you do with the function (that the bind returned), this will always be a

and there exists a proposal to make a::x a shortcut for a.x.bind(a) (except a is only evaluated once)