javascript - nodejs function to string containing function's code - Stack Overflow

admin2025-04-10  0

I am really really new to nodejs. Is there any way to convert function content to string? Something like, if I have:

function() {
    ....
}

I'd like to have "function() { .... }".

Is such thing possible?

I am really really new to nodejs. Is there any way to convert function content to string? Something like, if I have:

function() {
    ....
}

I'd like to have "function() { .... }".

Is such thing possible?

Share Improve this question edited Apr 3, 2013 at 17:10 djechlin 60.9k39 gold badges171 silver badges300 bronze badges asked Apr 3, 2013 at 17:09 NaorNaor 24.1k50 gold badges156 silver badges270 bronze badges 1
  • Did you try (function() {...}).toString()? – Benoir Commented Apr 3, 2013 at 17:10
Add a ment  | 

1 Answer 1

Reset to default 7

Functions already have a toString() method... so just (function() {}).toString() should work.

For example, in the node REPL:

> (function() { console.log("hello"); }).toString()
'function () { console.log("hello"); }'

Here's a link to some documentation.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744284817a239661.html

最新回复(0)