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 #
- Chrome: 自 66 版本开始支持
- Firefox: 支持
- Safari: 不支持
- Node.js: 自 8 版本开始支持
- Babel: 不支持