★with文

戻る with文 進む

ここでは、with文についてまとめています。ご参考になれば幸いです。

オブジェクトの省略 with文

with文は、オブジェクトの記述を省略するときに使用します。

with (省略オブジェクト) { 処理 }
と書きます。

ソース
<script type="text/JavaScript">
<!--
function randomVal_odd(){
randomVal = Math.floor(Math.random() * 100); //0から100未満の乱数をrandomValに代入
randomVal = randomVal | 1;		//乱数を1の論理和で奇数にしてrandomValに代入
return randomVal;
 			}
Val = "" + randomVal_odd();
with (document){ 				//書き出し
  write("100未満のランダムな奇数です。<br>");
  write(Val.fontcolor("red"));
}
//-->
</script>
<noscript>
JavaScriptが使えません。JavaScriptを無効にしているか、
JavaScriptに対応していないブラウザです。
</noscript>
Math.random()で乱数を発生させ、その値をMath.floor()で切り下げて、論理和で奇数にしdocument.write()で書き出しています。 本来document.write()と記述しますがwithdocument.の部分を省略しています。
参考.
fontエレメント
--------------------------------
表示
--------------------------------

[先頭へ] [Top Pageへ] [Contentsへ]


Copyright (C) pokochan 2000-2003
http://www.tees.ne.jp/~pokochan/java_script/with.html
E-mailpokochan@mx1.tees.ne.jp