javascript - Turn script content of a function into string -
this question has answer here:
- how functions's body string? 2 answers
for example, if have this:
function hello() {console.log("hello")}
i want able make function in java script return string value:
"console.log("hello");"
is there way plain javascript?
you can of code including function declaration calling tostring()
method on function. can parse string remove unwanted information.
something this:
function hello() { console.log("hello"); } var f = hello.tostring();//get string of whole function f = f.substring(f.indexof('{') + 1);//remove declaration , opening bracket f = f.substring(0, f.length - 1);//remove closing bracket f = f.trim();//remove starting/eding whitespace console.log(f);
Comments
Post a Comment