[ new ] Function【コンストラクタ】
bind【関数生成】
call【関数呼出し (可変引数)】
apply【関数呼出し (配列引数)】
toString【文字列変換】

[ new ] Function 【コンストラクタ】

メモ

構文

[ new ] Function( p1, p2, ..., pn, body )

Function【関数】オブジェクト
pn引数名 (全てをカンマ区切りの1つの文字列とみなす、例 参照)
body実行コード

SyntaxError 例外引数 または 実行コード が構文エラー

// 下記の3つの関数は同等
const func1 = Function("x", "y", "z", "return x + y + z;");
const func2 = new Function("x, y, z", "return x + y + z;");
const func3 = Function("x, y", "z", "return x + y + z;");
console.log(func1(1, 2, 3));  // 出力:6
console.log(func2(1, 2, 3));  // 出力:6
console.log(func3(1, 2, 3));  // 出力:6

// 引数なし関数
const funcA = Function("return 'RETURN';");
console.log(funcA());  // 出力:RETURN

// 例外
try {
  const funcB = Function("123", "return;");  // 引数不正
} catch (error) {
  console.log(error);  // 出力:SyntaxError: Unexpected number
}

try {
  const funcC = new Function("x", "/");  // 実行コード不正
} catch (error) {
  console.log(error);  // 出力:SyntaxError: Invalid regular expression: missing /
}

Function.prototype.bind【関数生成】

メモ

構文

function.bind( thisArg, ...args )

生成された関数
thisArg関数内でthisで参照される値 (省略 または null:グローバルオブジェクト)
args関数の引数

TypeError 例外関数が呼び出し不可

// this.min ~ this.max の間の引数を合計
const func = function() {
  let sum = 0;
  for (let i = 0; i < arguments.length; i++) {
    if ((this.min <= arguments[i]) && (arguments[i] <= this.max)) {
      sum += arguments[i];
    }
  }
  return sum;
}

// 特定の this 値(範囲:10~30)を設定した関数を作成
const rangeA = { min:10, max:30 };
const funcA = func.bind(rangeA);
console.log(funcA(5, 15, 25, 35)); // 出力:40 (15 + 25)

// 特定の this 値(範囲:5~25)・特定の引数(10, 20)を設定した関数を作成
const rangeB = { min:5, max:25 };
const funcB = func.bind(rangeB, 10, 20);
console.log(funcB(5, 15, 25, 35)); // 出力:75 ((10 + 20) + 5 + 15 + 25)

// 例外
function funcNon() { }
try {
  const funcNG = (funcNon + 123).bind(null);  // TypeError 例外
} catch (error) {
  console.log(error);  // 出力:TypeError: (funcNon + 123).bind is not a function
}

Function.prototype.call【関数呼出し (可変引数)】
Function.prototype.apply【関数呼出し (配列引数)】

メモ

  • 関数呼出し
    メソッド備考
    call【関数呼出し (可変引数)】引数を個々に指定し、関数呼出し
    apply【関数呼出し (配列引数)】引数を配列として指定し、関数呼出し
    • 他のオブジェクトのメソッド呼び出し可
  • 関連
  • 外部リンク (英語)
    Function.prototype.call ( thisArg, ...args )
    ES2023 (14) ES2022 (13) ES2021 (12)
    Function.prototype.apply ( thisArg, argArray )
    ES2023 (14) ES2022 (13) ES2021 (12)

構文

function.call( thisArg, ...args )
function.apply( thisArg, argArray )

指定関数の戻り値
thisArg関数内でthisで参照される値 (省略 または null:グローバルオブジェクト)
args関数の引数
argArray引数の配列オブジェクト (リテラル配列・Array【配列】オブジェクト等)

TypeError 例外
関数が呼び出し不可
argArrayがオブジェクト以外

const funcA0 = Function("return 999;");
const funcA1 = Function("x", "return x;");
const funcA2 = Function("x, y", "return x + y;");

const funcB0 = Function("return parseInt(this);");
const funcB1 = Function("x", "return this + x;");
const funcB2 = Function("x, y", "return this + x + y;");

console.log(funcA0.call(null));        // 出力:999
console.log(funcA1.call(null, 1));     // 出力:1
console.log(funcA2.call(null, 1, 2));  // 出力:3

console.log(funcB0.call(100));         // 出力:100
console.log(funcB1.call(100, 1));      // 出力:101
console.log(funcB2.call(100, 1, 2));   // 出力:103
const funcA = function(x, y, z) {
  return x + y + z;
}

const funcB = function(x, y, z) {
  return this + x + y + z;
}

console.log(funcA.apply(null, [1, 2, 3]));    // 出力:6
console.log(funcB.apply(100, [1, 2, 3]));     // 出力:106
console.log(Math.max.call(null, 1, 2, 3));    // 出力:3
console.log(Math.max.apply(null, [1, 2, 3])); // 出力:3

Function.prototype.toString【文字列変換】

メモ

  • 文字列に変換
    • 実装依存
    • コメント・空白・改行 等を削除することなく、定義したままを返却
  • 外部リンク (英語)
    Function.prototype.toString ( )
    ES2023 (14) ES2022 (13) ES2021 (12)

構文

function.toString()

変換文字列

function /* コメント */
func    (x, y) {
  return x + y;
}

console.log(func.toString());
// 出力:function /* コメント */
// 出力:func    (x, y) {
// 出力:  return x + y;
// 出力:}

// 参考 ES2018 (実装依存)
// 出力:function func(x, y) {
// 出力:  return x + y;
// 出力:}