Why is it when you call toString() on a function using quokka, it returns a string with extra variables added?

67 Views Asked by At

I am teaching myself to use prototypes to extend the functionality of objects. I have hit a roadblock when it comes to functions though, when I log a function to string it returns something I don't understand. For example, given:

function foo() {
    var a = 1
    var b = 2
    return a + b
}
console.log( foo.toString() )

you get:

`function foo() { 
    var $_$c = $_$wf(1); 
    var a = ($_$w(1, 130, $_$c), 1); 
    var b = ($_$w(1, 131, $_$c), 2); 
    return $_$w(1, 132, $_$c), a + b; 
}`

I would expect to get:

`function foo() {
    var a = 1
    var b = 2
    return a + b
}`

Why?

Edit screenshot

2

There are 2 best solutions below

3
On

try this:

function foo() {
    var a = 1
    var b = 2
    return a + b
}

console.log(foo);

let x = foo.toString();
console.log(typeof x) // This should return a string

When you print 'x' it should give you the code as a string too.

1
On

I try this example it return

`function foo() { 
    var $_$c = $_$wf(1); 
    var a = ($_$w(1, 130, $_$c), 1); 
    var b = ($_$w(1, 131, $_$c), 2); 
    return $_$w(1, 132, $_$c), a + b; 
}`

and then I made an object and put the function to it and try to return it as toString and also it work correctly