[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Function/toString [Back]  [Original]

Function.prototype.toString() - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

Function.prototype.toString()

Baseline

20157

toString() Function

function sum(a, b) {
  return a + b;
}

console.log(sum.toString());
// : "function sum(a, b) {
//                     return a + b;
//                   }"

console.log(Math.abs.toString());
// : "function abs() { [native code] }"

js
toString()

Function Object toString Object.prototype.toString Function toString

JavaScript toString

this Function toString() TypeError ("Function.prototype.toString called on incompatible object")

js
Function.prototype.toString.call("foo"); // TypeError 

toString() Function.prototype.bind() JavaScript toString()

function someName() { [native code] }

someName [1 + 1]someName1

: eval()

toString() Function toString() "anonymous" Function("a", "b", "return a + b").toString()

function anonymous(a,b
) {
return a + b
}

ES2018 toString()

toString

js
function test(fn) {
  console.log(fn.toString());
}

function f() {}
class A {
  a() {}
}
function* g() {}

test(f); // "function f() {}"
test(A); // "class A { a() {} }"
test(g); // "function* g() {}"
test((a) => a); // "(a) => a"
test({ a() {} }.a); // "a() {}"
test({ *a() {} }.a); // "*a() {}"
test({ [0]() {} }[0]); // "[0]() {}"
test(Object.getOwnPropertyDescriptor({ get a() {} }, "a").get); // "get a() {}"
test(Object.getOwnPropertyDescriptor({ set a(x) {} }, "a").set); // "set a(x) {}"
test(Function.prototype.toString); // "function toString() { [native code] }"
test(function f() {}.bind(0)); // "function () { [native code] }"
test(Function("a", "b")); // function anonymous(a\n) {\nb\n}

Function.prototype.toString() toString() Function eval()

js
function foo() {
  return "bar";
}
console.log(`${foo}`);
// function foo() {
//   return "bar";
// }

js
function foo /* a comment */() {
  return "bar";
}
console.log(foo.toString());
// function foo /* a comment */() {
//   return "bar";
// }

ECMAScript 2027 LanguageSpecification
# sec-function.prototype.tostring


Web Proxy Viewer  |  New URL  |  Original Page