Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

One additional point the author didn't mention:

Executing JavaScript code sent from the server requires your JS objects to be in the global scope. Returning JSON (on the other hand) allows you to keep your objects and logic inside a closure.



You can serve JS code that is wrapped in an anonimous function itself.


Yes, but that doesn't actually solve the problem. How is that code going to interact with your existing objects unless those objects are in the global scope?

If both the requester of the AJAX call and the receiver are in the same closure, there's no need for those objects to be globally accessible.


It is possible , e.g. if your javascript on the page is already using a common js module system, you can simply "require" the necessary modules. In addition, you can have a global pub/sub system in place (that you can "require" from you inlined script) and fire events. This will really help for decoupling. Usually you need inlined js along with the served html so that you attach behavior from within that inlined script. This is again easy to do from a "closured" piece of code.


yes this is another good reason to avoid it




Consider applying for YC's Winter 2027 batch! Applications are open till November 2.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: