今回はJavaScriptの組み込みオブジェクトの1つについて解説をします。
実行されているECMAScriptによっては実行できないものがありますので注意しましょう
数学関連の機能を持つオブジェクトです。
Mathオブジェクトはインスタンス化せずにそのまま使用します。
ECMAScript v1 に対応している機能は以下のとおりです。
var num = -10; document.writeln(Math.abs(num));
var num1 = 100; var num2 = 50; document.writeln(Math.max(num1, num2));
var num1 = 100; var num2 = 50; document.writeln(Math.min(num1, num2));
var num1 = 100; document.writeln(Math.pow(num1,3));
document.writeln(Math.random());
document.writeln(Math.ceil(100.5)); document.writeln(Math.ceil(100.02)); document.writeln(Math.ceil(-100.02));
document.writeln(Math.floor(100.5)); document.writeln(Math.floor(100.02)); document.writeln(Math.floor(-100.02));
document.writeln(Math.round(100.5)); document.writeln(Math.round(100.02)); document.writeln(Math.round(-100.02));
document.writeln(Math.SQRT1_2);
document.writeln(Math.SQRT2);
document.writeln(Math.sqrt(5));
document.writeln(Math.PI);
document.writeln(Math.cos(0.5));
document.writeln(Math.sin(0.5));
document.writeln(Math.tan(0.5));
document.writeln(Math.acos(1));
document.writeln(Math.asin(1));
document.writeln(Math.atan(1));
document.writeln(Math.atan2(1,1));
document.writeln(Math.E);
document.writeln(Math.LN2);
document.writeln(Math.LN10);
document.writeln(Math.LOG2E);
document.writeln(Math.LOG10E);
document.writeln(Math.log(10));
document.writeln(Math.exp(1));
ECMAScript v3 に対応している機能は以下のとおりです。
// v1だと引数に2つしか設定することができなかったがv3で機能拡張 document.writeln(Math.max(5,10,25,30));
// v1だと引数に2つしか設定することができなかったがv3で機能拡張 document.writeln(Math.min(5,10,25,30));
日付 | 詳細 |
---|---|
2011/09/30 | コンテンツ公開 |
Copyright (C) 2011 ほぷしぃ. All Rights Reserved.