Get to know MDN better
Esta página foi traduzida do inglês pela comunidade. Saiba mais e junte-se à comunidade MDN Web Docs.
This feature is well established and works across many devices and browser versions. It’s been available across browsers since julho de 2015.
O método toString() retorna uma string representando o código fonte da função.
Uma string representando o código fonte da função.
O objeto da Function substitui o método toString herdado de Object; ele não herda Object.prototype.toString. Para objetos Function definidos pelo usuário, o método toString retorna uma string contendo o seguimento de texto de origem que foi usado para definir a função
O JavaScript chama o método toString automaticamente quando uma Function pode ser representada como um valor de texto. e.x. quando uma função é concatenada com uma string.
O método toString() lançará uma exceção do tipo TypeError ("Function.prototype.toString called on incompatible object") se o valor this do objeto não é um objeto do tipo Function.
Se o método toString() é chamado por objetos de funções embutidas ou por uma função criada por Function.prototype.bind, toString() retorna uma string de uma função nativa que parece
Se o método toString() é chamado por uma função criada pelo contrutor de Function, toString() retorna o código fonte de uma declaração de função sintetizada chamada "anonymous" usando os parâmetros passados e o corpo da função.
| function f(){} | "function f(){}" |
| class A { a(){} } | "class A { a(){} }" |
| function* g(){} | "function* g(){}" |
| a => a | "a => a" |
| ({ a(){} }.a) | "a(){}" |
| ({ *a(){} }.a) | "*a(){}" |
| ({ [0](){} }[0]) | "[0](){}" |
| Object.getOwnPropertyDescriptor({ get a(){} }, "a").get | "get a(){}" |
| Object.getOwnPropertyDescriptor({ set a(x){} }, "a").set | "set a(x){}" |
| Function.prototype.toString | "function toString() { [native code] }" |
| (function f(){}.bind(0)) | "function () { [native code] }" |
| Function("a", "b") | "function anonymous(a\n) {\nb\n}" |
| ECMAScript® 2027 Language Specification # sec-function.prototype.tostring |
Enable JavaScript to view this browser compatibility table.
This page was last modified on 17 de fev. de 2025 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.