0

I saw this code from this site:

websocket_client.on( "eval_callback",function(data){data=atob(data),eval(data)}.bind() ) ;

Specifically I'm wondering about

function(data){data=atob(data),eval(data)}.bind()

I'm wondering if there is a purpose to call .bind() with no arguments.

Buge
  • 476
  • 3
  • 14
  • [Somewhat related](https://stackoverflow.com/questions/11107823/what-happens-if-i-dont-pass-a-parameter-in-a-javascript-function). – user202729 Mar 04 '18 at 07:19

1 Answers1

5

Calling .bind() with no arguments is equivalent to calling it with the argument undefined. This would be useful if you want to ensure that the function has no context if it tries to use this.

In your example,the function calls eval(), so I guess they want to ensure that the evaluation doesn't have any this context.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • This answer explains bind with some examples https://stackoverflow.com/questions/2236747/use-of-the-javascript-bind-method – Zim84 Mar 04 '18 at 07:16
  • [Which is also equivalent to `bind(global)`](https://tio.run/##VY4xDoMwDEX3nMLqlAxFnQvhLoHENCiyK0JZUM@eBgJS8WT//@zv0Swm9pN/z3di61LCD/WzZwKUahUe5fzyUWs9BO5MUD1T5OCqwIO8tW1Rm@amauFCdPDvb5tZ/wrB3ahXpCfmPt/d5gopN2IxE@RI6xB0UavOk5W75MlZVR92pne4RF7g47ca4LRP@AFbXeAdK3XYUqX0Aw). – user202729 Mar 04 '18 at 07:17