javascript - Turn script content of a function into string -


this question has answer here:

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); 

here working example


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -