Revised Function.prototype.toString

Published · Tagged with ECMAScript ES2019

Function.prototype.toString() now returns exact slices of source code text, including whitespace and comments. Here’s an example comparing the old and the new behavior:

// Note the comment between the `function` keyword
// and the function name, as well as the space following
// the function name.
function /* a comment */ foo () {}

// Previously, in V8:
foo.toString();
// → 'function foo() {}'
// ^ no comment
// ^ no space

// Now:
foo.toString();
// → 'function /* comment */ foo () {}'

Feature support #