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

jQuery taught me how to write JavaScript frameworks. John Resig, the founder of jQuery, wrote a book called "Secrets of the JavaScript Ninja" was back then really a mindbender.

Function.prototype.toString() is really the craziest thing I know to date regarding JS. Heavily used in earlier versions of jQuery according to the book.



Please elaborate about toString() ?


The toString() method returns a string representing the source code of the function.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...


That's pretty neat. Thanks for the link.


If called on a Function it returns the source of the function, which allows for all sorts of meta programming.


If memory serves, a real world example was Angular doing a toString then injecting dependencies before calling eval on the new code.

To be honest, I don’t know that there’s ever a good reason to use it in production as it almost always involves performance and security problems.


> involves performance [...] problems

Except when it solves them! https://mrale.ph/blog/2018/02/03/maybe-you-dont-need-rust-to...


I once used it for a proof of concept for computer worm type message on a sort of message board. Used to re-generate the source code to be added to new messages.


Yep. We used to use this for some hacks back before AST libraries were a thing, mainly because Function.prototype.toString() preserves comments.

function foo() { /* a comment! / }

console.log(foo.toString()); //-> "function foo() { / a comment! */ }"




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

Search: