概要
JavaScript における関数の全ては、実際には Function オブジェクトです。
構文
new Function ([arg1[, arg2[, ... argN]],] functionBody)
引数
-
arg1, arg2, ... argN -
仮引数の名前として関数で用いるための名前。各々は、妥当な JavaScript の識別子と一致する文字列か、コンマで区切られたそのような文字列のリストでなければなりません。例えば、"
x"、"theValue" 、"a,b。 -
functionBody - 関数定義を形成する JavaScript の文を含む文字列。
説明
Function コンストラクタで生成された Function オブジェクトは、関数が作成されたときにパースされます。これは、関数を宣言し、それをコード内で呼び出すよりも効果的ではありません。なぜなら、function 文で宣言された関数はコードの他の部分と一緒にパースされるからです。
関数に渡された実引数の全ては、それらが渡された順番に、生成された関数内における仮引数の識別子の名前と同等に扱われます。
Note: Functions created with the Function constructor do not create closures to their creation contexts; they always run in the window context (unless the function body starts with a"use strict"; statement, in which case the context is undefined).
new 演算子を用いずに)関数として、Function コンストラクタを実行することは、コンストラクタとして実行することと同じです。プロパティ
Function インスタンスから継承されているプロパティについては、Function インスタンスのプロパティを参照してください。
- prototype
-
Functionオブジェクトの全ての拡張を可能にします。
メソッド
Function インスタンスから継承されているメソッドについては、Function インスタンスのメソッドを参照してください。Function オブジェクトは、自分自身のメソッドを持っていませんが、プロトタイプチェーンを通していくつかのメソッドを継承しています。
Function インスタンス
Function インスタンスは Function.prototype を継承します。 すべてのコンストラクタと同様に、コンストラクタのプロトタイプオブジェクトを変更することで、すべての Function インスタンス が変更が加えられます。
プロパティ
- arguments Deprecated
-
An array corresponding to the arguments passed to a function. This is deprecated as property of
Function, use the arguments object available within the function instead.
- arity Obsolete since JavaScript 1.8.6
- Specifies the number of arguments expected by the function. Use the length property instead.
- caller Non-standard
- Specifies the function that invoked the currently executing function.
- constructor
- Specifies the function that creates an object's prototype.
- length
- Specifies the number of arguments expected by the function.
- name Non-standard
- The name of the function.
メソッド
-
apply - Applies the method of another object in the context of a different object (the calling object); arguments can be passed as an Array object.
-
bindRequires JavaScript 1.8.5 - Creates a new function which, when called, itself calls this function in the context of the provided value, with a given sequence of arguments preceding any provided when the new function was called.
-
call - Calls (executes) a method of another object in the context of a different object (the calling object); arguments can be passed as they are.
-
isGeneratorRequires JavaScript 1.8.6 -
Returns
trueif the function is a generator; otherwise returnsfalse. -
toSourceNon-standard -
Returns a string representing the source code of the function. Overrides the
Object.toSourcemethod. -
toString -
Returns a string representing the source code of the function. Overrides the
Object.toStringmethod.
例
例: Function コンストラクタの引数を指定する
次のコードは、2 つの引数を受け取る Function オブジェクトを生成します。
var multiply = new Function("x", "y", "return x * y");
var theAnswer = multiply(7, 6);
引数 "x" と "y" は、"return x * y" という関数の中身において使用される仮引数の名前です。
Mozilla Developer Network