「JavaScript」Function.prototype.apply()のサンプル

書式
func.apply(thisArg, [ argsArray])
引数 thisArg
this の値で、 func の呼び出しで提供されます。

argsArray
1つの配列風のオブジェクトであり、 func 関数が呼ぶことになる引数を列挙したものです。
関数に引数が渡されない場合は null または undefined となります。

戻り値
指定した this と引数で関数を呼び出した結果が返ります。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const nm = [15, 26, 32, 43, 57];
const max = Math.max.apply(null, nm);
console.log("最大値: "+max);
const min = Math.min.apply(null, nm);
console.log("最小値: "+min);
const nm = [15, 26, 32, 43, 57]; const max = Math.max.apply(null, nm); console.log("最大値: "+max); const min = Math.min.apply(null, nm); console.log("最小値: "+min);
const nm = [15, 26, 32, 43, 57];

const max = Math.max.apply(null, nm);

console.log("最大値: "+max);

const min = Math.min.apply(null, nm);

console.log("最小値: "+min);

実行結果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
> "最大値: 57"
> "最小値: 15"
> "最大値: 57" > "最小値: 15"
> "最大値: 57"
> "最小値: 15"

 

JavaScript

Posted by arkgame