2

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?

Ofri
  • 474
  • 3
  • 13
  • 1
    1) `toSource()` is IE-only, see answers to [this question](http://stackoverflow.com/questions/171407/implementing-mozillas-tosource-method-in-internet-explorer) for implementations in other browsers. 2) what is the connection of `toSource` to the questions you are asking? – Julian D. Jan 22 '12 at 13:06
  • 1
    I suggest you don't use eval(); it is slow and if code is user-generated you risk XSS. – Fabio Buda Jan 22 '12 at 13:19
  • Thanks. My question is more theoretic.. I didn't mean to suggest the actual why of doing it (the toSource is just a possible way to serialize a function to be transferred over the network). – Ofri Jan 22 '12 at 14:03

2 Answers2

0

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.

bigblind
  • 11,435
  • 13
  • 61
  • 111
  • Thanks! yes, I thought about what you are saying, and that leads me to my next question/wonder: is it possible to transfer a function with it's closure? Do you think that kind of feature will be useful? – Ofri Jan 22 '12 at 14:07
0

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.

Andreas Köberle
  • 88,409
  • 51
  • 246
  • 277