You’re going to need to write a custom serializer similar to util.inspect
to get this. Be forewarned, though, there’s a reason functions aren’t
displayed as their source by default: calling .toString() on a function
doesn’t include the closure environment for the function, so any lexically
defined values that are in scope when the function was defined won’t be
available later:

function mildCurry (a) {
  return (b) => a + b;
}

const oneOff = { add2: mildCurry(2) }
// > oneOff.add2.toString()
// '(b) => a + b'

If you’re careful about how you define functions on objects (and also
figure out whether you want to include prototype methods as part of this
serialization) you can probably stay out of trouble, but this isn’t as
generally useful as you might hope.

On Mon, Jan 28, 2019 at 12:05 PM Aaron Gray <aaronngray.li...@gmail.com>
wrote:

> Hi,
>
> I need to print out objects with embedded functions ideally both to the
> command line and as files.
>
>     const test = {
>         name: "test",
>         get: function() { return "test"; }
>     };
>
> I need to print out a structure like the above with the function
> definition to the console and also as a text file.
>
> Regards,
> Aaron
>
>
> --
> Aaron Gray
>
> Independent Open Source Software Engineer, Computer Language Researcher,
> Information Theorist, and amateur computer scientist.
>
> --
> Job board: http://jobs.nodejs.org/
> New group rules:
> https://gist.github.com/othiym23/9886289#file-moderation-policy-md
> Old group rules:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> ---
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to nodejs+unsubscr...@googlegroups.com.
> To post to this group, send email to nodejs@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/nodejs/CANkmNDecj0Mac9mXyh0-hpDdubMxZvT2Rdmp-4DK-KiOAZ-E-w%40mail.gmail.com
> <https://groups.google.com/d/msgid/nodejs/CANkmNDecj0Mac9mXyh0-hpDdubMxZvT2Rdmp-4DK-KiOAZ-E-w%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to nodejs+unsubscr...@googlegroups.com.
To post to this group, send email to nodejs@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/CAHG3qKooLfAwJf-98fFQwWP8xLg_szZTcFm6giasLcStBG8OaQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to