Skip to content Skip to sidebar Skip to footer

How Do I Bind A Mapped Function?

I'm just messing with some code here to better understand JavaScript so don't ask me why I want to do this or provide alternate solutions. I'm looking for a reasoning of why this i

Solution 1:

hello.print = Object.keys(hello).map.bind(hello, print, hello);

Now, I suppose that should (in my knowledge and opinion) bind a version of map (which calls print on hello and obviously bind also takes a value for this so I've sent in hello twice) to the object.

Yes, and it does. Only that it does bindmap to hello, while the original call does apply map on the Object.keys(hello) array - it's this value in the method call. So you need to do

hello.print = Array.prototype.map.bind(Object.keys(hello), print, hello);

Post a Comment for "How Do I Bind A Mapped Function?"