I need something like heredoc in JavaScript. Do you have any ideas for this? I need cross-browser functionality.
I found this:
heredoc = \'\\
Building on Zv_oDD's answer, I created a similar function for easier reuse.
Warning: This is a non-standard feature of many JS interpreters, and will probably be removed at some point, but as I'm building a script to be only used in Chrome, I am using it! Do not ever rely on this for client-facing websites!
// Multiline Function String - Nate Ferrero - Public Domain
function heredoc(fn) {
return fn.toString().match(/\/\*\s*([\s\S]*?)\s*\*\//m)[1];
};
Use:
var txt = heredoc(function () {/*
A test of horrible
Multi-line strings!
*/});
Returns:
"A test of horrible
Multi-line strings!"
Notes:
Edits:
2/2/2014 - changed to not mess with the Function prototype at all and use the name heredoc instead.
5/26/2017 - updated whitespace to reflect modern coding standards.