Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Javascript get a function as text?

Can Javascript get a function as text? I'm thinking like the inverse of eval().

function derp() { a(); b(); c(); }  alert(derp.asString()); 

The result would be something like "a(); b(); c();"

Does it exist?

like image 653
Brandon Avatar asked Jul 31 '10 21:07

Brandon


People also ask

How do you turn a function into a string in JavaScript?

To convert a function to string, use the toString() method of a function object.

Can we write function in JavaScript?

In JavaScript, we can make functions that are able to return a value. To create such type of function, we have to use the return statement, but it must be the last statement in the body of the function (or in the definition of the function).

How do you call a function in a string?

There are two methods to call a function from string stored in a variable. The first one is by using the window object method and the second one is by using eval() method. The eval() method is older and it is deprecated.

How do you display a function in JavaScript?

You can display output in JavaScript using any of the following ways: To display output in the browser console, you can use the “console. log()” function. To write out into HTML and display its output, you can use the “document.

What is a function in JavaScript?

In JavaScript, a function is a value, so we can deal with it as a value. The code above shows its string representation, which is the source code. Surely, a function is a special value, in the sense that we can call it like sayHi ().

What does JavaScript do before it runs?

When JavaScript prepares to run the script, it first looks for global Function Declarations in it and creates the functions. We can think of it as an “initialization stage”.

Is there a way to get the text of a function?

The closest thing available to what you're after is calling .toString () on a function to get the full function text, like this: You can give it a try here, some caveats to be aware of though:

Why can’t I use JavaScript functions outside of ask?

Such functions are not accessible outside of ask (because they are not assigned to variables), but that’s just what we want here. Such code appears in our scripts very naturally, it’s in the spirit of JavaScript. Regular values like strings or numbers represent the data.


2 Answers

Updated to include caveats in the comments below from CMS, Tim Down, MooGoo:

The closest thing available to what you're after is calling .toString() on a function to get the full function text, like this:

function derp() { a(); b(); c(); } alert(derp.toString()); //"function derp() { a(); b(); c(); }" 

You can give it a try here, some caveats to be aware of though:

  • The .toString() on function is implementation-dependent (Spec here section 15.3.4.2)
    • From the spec: An implementation-dependent representation of the function is returned. This representation has the syntax of a FunctionDeclaration. Note in particular that the use and placement of white space, line terminators, and semicolons within the representation string is implementation-dependent.
    • Noted differences in Opera Mobile, early Safari, neither displaying source like my example above.
  • Firefox returns a compiled function, after optimization, for example:
    • (function() { x=5; 1+2+3; }).toString() == function() { x=5; }
like image 91
Nick Craver Avatar answered Sep 29 '22 10:09

Nick Craver


function derp() { a(); b(); c(); }  alert(derp.toString()); 
like image 44
CD.. Avatar answered Sep 29 '22 10:09

CD..