Passing on a function from server to client

208 Views Asked by At

I recently ran into the foo.toSource() option in JavaScript. This allows a serialization of a function. I was wondering about the possibilities of transferring functions over the networks, from server to client, and executing them at a later time - with eval().

Do you think it's actually useful?
Are there any frameworks using such a paradigm?

2

There are 2 best solutions below

1
On

The only use cases I see are:

  1. whenever you send .js file
  2. JSNOP

The thing you thought about isn't posible. So we can only send the text representation of the function, not it's context or closures and all that stuff.

1
On

Passing functions from the server to a page can be done without toSource, by just putting it in the code when the page is generated on the server, or requesting it via AJAX when needed.