5

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?

1
  • Did you try (function() {...}).toString()? Commented Apr 3, 2013 at 17:10

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@kamituel I believe Function.prototype.toString() is standard. The indentation parameter referenced in the MDN docs is non-standard, but this solution doesn't rely on that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.