with()を使った計算

with()を使うとオブジェクト名のMathを省略できる。

result = Math.pow(3,2) * Math.PI

with(Math) {
var result = pow(3,2) * PI
}

は、resultに同じ値が入る。


実行結果


ヘッダ部分
なし

ボディ部分 <SCRIPT LANGUAGE="JavaScript"> var result1 = Math.pow(3,2) * Math.PI with(Math) { var result2 = pow(3,2) * PI } document.write("半径3cmの円の面積は" + result1 + "cm2"+ "です。<BR>") document.write("半径3cmの円の面積は" + result2 + "cm2"+ "です。<BR>") </SCRIPT>