function derp() { a(); b(); c(); }
derp.toString()
will return "function derp() { a(); b(); c(); }"
, but I only need the body of the function, so "a(); b(); c();"
, because I can then evaluate the expression. Is it possible to do this in a cross-browser way?
clone = function() { var newfun = new Function('return ' + this. toString())(); for (var key in this) newfun[key] = this[key]; return newfun; };
functions are data in memory stack, so when you define another function with the same name, it overrides the previous one. Show activity on this post. Well obviously you're not meant to define the same function twice. However, when you do, the latter definition is the only 1 that applies.
The $() function The dollar function, $(), can be used as shorthand for the getElementById function. To refer to an element in the Document Object Model (DOM) of an HTML page, the usual function identifying an element is: document. getElementById("id_of_element").
The toString() method returns a string representing the source code of the specified Function .
var entire = derp.toString(); var body = entire.slice(entire.indexOf("{") + 1, entire.lastIndexOf("}")); console.log(body); // "a(); b(); c();"
Please use the search, this is duplicate of this question
Since you want the text between the first {
and last }
:
derp.toString().replace(/^[^{]*{\s*/,'').replace(/\s*}[^}]*$/,'');
Note that I broke the replacement down into to regexes instead of one regex covering the whole thing (.replace(/^[^{]*{\s*([\d\D]*)\s*}[^}]*$/,'$1')
) because it's much less memory-intensive.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With