| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Function/toString | [Back] [Original] |
Get to know MDN better
Esta pgina foi traduzida do ingls pela comunidade. Saiba mais e junte-se comunidade MDN Web Docs.
This feature is well established and works across many devices and browser versions. Its been available across browsers since julho de 2015.
O mtodo toString() retorna uma string representando o cdigo fonte da funo.
function sum(a, b) {
return a + b;
}
console.log(sum.toString());
// Expected output: "function sum(a, b) {
// return a + b;
// }"
console.log(Math.abs.toString());
// Expected output: "function abs() { [native code] }"
function.toString()
Uma string representando o cdigo fonte da funo.
O objeto da Function substitui o mtodo toString herdado de Object; ele no herda Object.prototype.toString. Para objetos Function definidos pelo usurio, o mtodo toString retorna uma string contendo o seguimento de texto de origem que foi usado para definir a funo
O JavaScript chama o mtodo toString automaticamente quando uma Function pode ser representada como um valor de texto. e.x. quando uma funo concatenada com uma string.
O mtodo toString() lanar uma exceo do tipo TypeError ("Function.prototype.toString called on incompatible object") se o valor this do objeto no um objeto do tipo Function.
Function.prototype.toString.call("foo"); // TypeError
Se o mtodo toString() chamado por objetos de funes embutidas ou por uma funo criada por Function.prototype.bind, toString() retorna uma string de uma funo nativa que parece
"function () {\n [native code]\n}";
Se o mtodo toString() chamado por uma funo criada pelo contrutor de Function, toString() retorna o cdigo fonte de uma declarao de funo sintetizada chamada "anonymous" usando os parmetros passados e o corpo da funo.
| Funo | Function.prototype.toString resultado |
|---|---|
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}" |
| Specification |
|---|
| ECMAScript 2027 LanguageSpecification # sec-function.prototype.tostring |
Function.prototype.toString() tem sido implementada salvando o fonte da funo. O descompilador foi removido o que torna o parmetro indentation desnecessrio. Veja Erro do Firefox 761723 para mais detalhes.Function.prototype.toString() lanava excees para objetos Proxy (Erro do Firefox 1100936 e Erro do Firefox 1440468).This page was last modified on 17 de fev. de 2025 by MDN contributors.
FunctionObject/FunctionObject.prototype.__defineGetter__()Object.prototype.__defineSetter__()Object.prototype.__lookupGetter__()Object.prototype.__lookupSetter__()Object.prototype.hasOwnProperty()Object.prototype.isPrototypeOf()Object.prototype.propertyIsEnumerable()Object.prototype.toLocaleString()Object.prototype.toString()Object.prototype.valueOf()Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |