File size: 1,094 Bytes
bc20498
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# to-json-callback

[![build status](https://github.com/WebReflection/to-json-callback/actions/workflows/node.js.yml/badge.svg)](https://github.com/WebReflection/to-json-callback/actions) [![Coverage Status](https://coveralls.io/repos/github/WebReflection/to-json-callback/badge.svg?branch=main)](https://coveralls.io/github/WebReflection/to-json-callback?branch=main)

Did you know that some function in *JS* can't be serialized as string and be evaluated as such somewhere else?

If you did, you are in the right project, if you didn't though, you still are in the right project.

### what does this solve

```js
const obj = {method() {}};

const str = String(obj.method);
// method() {}

eval(str);
// Uncaught SyntaxError: Unexpected token '{'
```

now ... let's try this again:
```js
import toJSONCallback from 'to-json-callback';

const obj = {method() {}};

const str = toJSONCallback(obj.method);
// function method() {}

eval(str);
// no error whatsoever
```

This is also compatible with all `async` variant of functions, as 100% code-coverage test proves.

**And that's all folks!**