<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>我的普立茲 &#187; JavaScript</title>
	<atom:link href="http://blog.beyes.tw/tag/javascript/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.beyes.tw</link>
	<description>The Time Of My Life …</description>
	<lastBuildDate>Mon, 26 Jul 2010 22:32:31 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>物件模型的概念(The concept of Object-Model)</title>
		<link>http://blog.beyes.tw/337</link>
		<comments>http://blog.beyes.tw/337#comments</comments>
		<pubDate>Mon, 11 May 2009 22:33:37 +0000</pubDate>
		<dc:creator>skyfate</dc:creator>
				<category><![CDATA[JavaScript1.5]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://blog.beyes.tw/?p=337</guid>
		<description><![CDATA[JS 是一種以物件為導向的語言程式，所以理所當然的，這個語言就是以物件為中心，那這些物件又為何呢？除了由 JS 本身所提供的內建物件或者是我們所自定的物件之外，屬於瀏覽器內的相關元件也都是物件，諸如：document(文件)、window(視窗)…等等，另外在網頁中以 &#60;&#62; 所括住的其它標記語言標籤，也都可以將它視之為物件。
我們可以粗淺地這麼說——舉凡能在瀏覽器上使用到的元件，我們都可以將之視為物件。
物件模型層次(The Object-Model Hierarchy)
文件的物件層次其基本的作用是用來提供 Script 在瀏覽器視窗所包含的物件中來引用相關的物件。層次就某個方面來說，它就是路徑，Script 可用它來精確定位或參考某個物件。在 JS 中，文件物件的層次是一種「包含」的層次，而不是「繼承」的層次，也就說沒有一個物件會繼承其高層物件所屬的方法或屬性。
當瀏覽器戴入一張網頁的同時，瀏覽器會依據標記語言文件與其它適當資訊的特定值，來建立一定數量的 JS 物件。而這些物件將會存在於標記語言文件它自身的層次結構中。
每張網頁皆有(至少)以下物件：

navigator(瀏覽器)：存放瀏覽器相關資訊與 plug-ins 資訊。
window(視窗)：有適用於整個視窗的資訊，框架文件中的每一個子視窗也是視窗物件。
document(文件)：包含依據文件內容的資訊，如：背景顏色、鏈結與表單等等。
location(位址)：當前網頁位址的資訊。
history(瀏覽記錄)：包含先前網頁位址的資訊。

依據它的內容，文件當中也可以包含其它的物件。例如在文件當中的每一個 &#60;form&#62; 標籤都會產生相對應的 form 物件。提到具體物件的定義，您必須指定物件的名稱和它所繼承的所有物件名。通常，在文件中物件的命名是由標記語言標籤所屬的標籤名稱來決定的。
那我們應該要如何使用正確的方式來指定或引用文件當中的目標物件呢？這時我們就要注意一下物件的層次然後再來指定或引用。現在，我們就使用下面較為通俗的例子來說明。
例如在一間「學校」裡頭，有許多間的「教室」，而教室裡有許多位的「學生」。在前述之中我們可以將「學校」當成是一個瀏覽的視窗，「教室」則可以將它視為一張張的網頁，而「學生」可將它當作是網頁中的某個物件。
如果我們要指定或引用某個教室裡的學生「王小明」，這時我們就要注意順序將它完整敘述出，而且須將「各部份之間」用半形句點 . 連接之︰
學校.3年3班.學生.王小明
我們也可以想像「王小明」具有某些特定的技術或方法來做動作：
學校.3年3班.學生.王小明.動作()
然而，這時王小明的動作並沒有完成，其動作的方法需要一個參數來指引它應該怎麼做動作。
學校.3年3班.學生.王小明.動作(起立)
這時，我們所指定的王小明完成了所有應有的動作。
由以上的說明中，我們可發現到，指定物件或引用物件的方式是由大到小或由上到下的，對於這種方式我們可以稱之為「物件的順序層次」，而在網頁之中也是以這種相同的方式來分層次的。假如某個物件的作用域越小，其物件位置的「假設路徑」就越多(因為我們所引用的物件也多)，對客戶端 JS 而言，其作用域最大是不會超過瀏覽器的。

左邊的圖是一個最低公用標準的文件物件層次圖，它可以在所有的瀏覽器當中施行。
例如，當我們想要來指定或者是引用 form 中的 button 物件時，其寫法為：
doucment.form.button

文件物件是如何產生的(How Document Objects Are Born)
當一份標記語言文件被瀏覽器載入後，瀏覽器會在記憶體裡保留由文件中標記語言標記語言的標籤來產生的物件模型，每個文件都有其相應的 window 物件。以下就是一個簡易的示範：




由於瀏覽器載入了該份文件，所以在當前的視窗之中，瀏覽器會在其模型中產生一個 document 物件。





然後我們可以在這份標記語言文件當中添加一個表單元素(也就是 form 標籤對)，然後我們再重新載入這份文件，此時瀏覽器的物件模型中則會產生一個 form 物件，而 form 物件在當前層次中是被包含在 document 物件中。





然後我們在此標記語言文件當中再添加一個文字輸入元素，重新載入該文件後，此時模型中的視窗則會包含一個文件，文件中包含一個表單，表單中則包含一個文字輸入元素。





然後我們在此標記語言文件當中再添加一個按鈕元素，然後重新載入該文件，因為文字輸入元素和按鈕元素是屬於同一層次的，所以在物件模型中的表現則是被包含在表單元素中。



物件的引用(The Object References)
每當一份標記語言文件載入瀏覽器後，該文件中的所有物件均會依據瀏覽器文件物件模型(Document Object Model，DOM)所規定的層次結構來存放這些物件。假如一個腳本(Script)要控制一個物件，就會有一個相對應的方式來指向一個物件，我們讓腳本來指出它所要的是那一個物件，這種方式我們則可稱之為「物件的引用」或「物件的參考」。
對於每個物件的引用，其引用的路徑(層次)必須包括從頂端的層次到目標物件的層次，而在這個路徑中，每個相連物件之間必須使用句號來隔開彼此。
以下就是一個簡易的 JS 物件引用的示範：




因為在每個視窗(或框架)中只能有一個文件，所以我們對於一個文件的引用方式為：
window.document




　



　



　


從上面我們可以瞭解到，我們引用一個層次越低的物件，其引用的路徑(層次)也就會越長。
※因為在正常的情況下每一個視窗它就只能有一份文件，所以我們對於物件的引用上可以省略 window 這個物件，然後直接從 document 開始引用。
屬性、方法與事件處理(Properties、Methods [...]]]></description>
			<content:encoded><![CDATA[<p>JS 是一種以物件為導向的語言程式，所以理所當然的，這個語言就是以物件為中心，那這些物件又為何呢？除了由 JS 本身所提供的內建物件或者是我們所自定的物件之外，屬於瀏覽器內的相關元件也都是物件，諸如：document(文件)、window(視窗)…等等，另外在網頁中以 <b class="MyColor1">&lt;&gt;</b> 所括住的其它標記語言標籤，也都可以將它視之為物件。</p>
<p>我們可以粗淺地這麼說——舉凡能在瀏覽器上使用到的元件，我們都可以將之視為物件。</p>
<h2>物件模型層次(The Object-Model Hierarchy)</h2>
<p>文件的物件層次其基本的作用是用來提供 Script 在瀏覽器視窗所包含的物件中來引用相關的物件。層次就某個方面來說，它就是路徑，Script 可用它來精確定位或參考某個物件。在 JS 中，文件物件的層次是一種「包含」的層次，而不是「繼承」的層次，也就說沒有一個物件會繼承其高層物件所屬的方法或屬性。</p>
<p>當瀏覽器戴入一張網頁的同時，瀏覽器會依據標記語言文件與其它適當資訊的特定值，來建立一定數量的 JS 物件。而這些物件將會存在於標記語言文件它自身的層次結構中。</p>
<p>每張網頁皆有(至少)以下物件：</p>
<ul>
<li>navigator(瀏覽器)：存放瀏覽器相關資訊與 plug-ins 資訊。</li>
<li>window(視窗)：有適用於整個視窗的資訊，框架文件中的每一個子視窗也是視窗物件。</li>
<li>document(文件)：包含依據文件內容的資訊，如：背景顏色、鏈結與表單等等。</li>
<li>location(位址)：當前網頁位址的資訊。</li>
<li>history(瀏覽記錄)：包含先前網頁位址的資訊。</li>
</ul>
<p>依據它的內容，文件當中也可以包含其它的物件。例如在文件當中的每一個 &lt;form&gt; 標籤都會產生相對應的 form 物件。提到具體物件的定義，您必須指定物件的名稱和它所繼承的所有物件名。通常，在文件中物件的命名是由標記語言標籤所屬的標籤名稱來決定的。</p>
<p>那我們應該要如何使用正確的方式來指定或引用文件當中的目標物件呢？這時我們就要注意一下物件的層次然後再來指定或引用。現在，我們就使用下面較為通俗的例子來說明。</p>
<p>例如在一間「學校」裡頭，有許多間的「教室」，而教室裡有許多位的「學生」。在前述之中我們可以將「學校」當成是一個瀏覽的視窗，「教室」則可以將它視為一張張的網頁，而「學生」可將它當作是網頁中的某個物件。</p>
<p>如果我們要指定或引用某個教室裡的學生「王小明」，這時我們就要注意順序將它完整敘述出，而且須將<b>「各部份之間」</b>用半形句點 <b class="MyColor1">.</b> 連接之︰</p>
<p><code class="MyCode0">學校.3年3班.學生.王小明</code></p>
<p>我們也可以想像「王小明」具有某些特定的技術或方法來做動作：</p>
<p><code class="MyCode0">學校.3年3班.學生.王小明.動作()</code></p>
<p>然而，這時王小明的動作並沒有完成，其動作的方法需要一個參數來指引它應該怎麼做動作。</p>
<p><code class="MyCode0">學校.3年3班.學生.王小明.動作(起立)</code></p>
<p>這時，我們所指定的王小明完成了所有應有的動作。</p>
<p>由以上的說明中，我們可發現到，指定物件或引用物件的方式是由大到小或由上到下的，對於這種方式我們可以稱之為「物件的順序層次」，而在網頁之中也是以這種相同的方式來分層次的。假如某個物件的作用域越小，其物件位置的「假設路徑」就越多(因為我們所引用的物件也多)，對客戶端 JS 而言，其作用域最大是不會超過瀏覽器的。</p>
<p><img src="http://i19.photobucket.com/albums/b152/skyfate/blog/id0337-OM-BrowserDocumentObject.png" alt="" align="left" /></p>
<p>左邊的圖是一個最低公用標準的文件物件層次圖，它可以在所有的瀏覽器當中施行。</p>
<p>例如，當我們想要來指定或者是引用 form 中的 button 物件時，其寫法為：</p>
<p><code class="MyCode0">doucment.form.button</code></p>
<div style="clear: both;"></div>
<h2>文件物件是如何產生的(How Document Objects Are Born)</h2>
<p>當一份標記語言文件被瀏覽器載入後，瀏覽器會在記憶體裡保留由文件中標記語言標記語言的標籤來產生的物件模型，每個文件都有其相應的 window 物件。以下就是一個簡易的示範：</p>
<table>
<tr>
<td><img src="http://i19.photobucket.com/albums/b152/skyfate/blog/id0337-OM-1.png" alt="" /></td>
<td>
<p>由於瀏覽器載入了該份文件，所以在當前的視窗之中，瀏覽器會在其模型中產生一個 document 物件。</p>
</td>
</tr>
<tr>
<td><img src="http://i19.photobucket.com/albums/b152/skyfate/blog/id0337-OM-2.png" alt="" /></td>
<td>
<p>然後我們可以在這份標記語言文件當中添加一個表單元素(也就是 form 標籤對)，然後我們再重新載入這份文件，此時瀏覽器的物件模型中則會產生一個 form 物件，而 form 物件在當前層次中是被包含在 document 物件中。</p>
</td>
</tr>
<tr>
<td><img src="http://i19.photobucket.com/albums/b152/skyfate/blog/id0337-OM-3.png" alt="" /></td>
<td>
<p>然後我們在此標記語言文件當中再添加一個文字輸入元素，重新載入該文件後，此時模型中的視窗則會包含一個文件，文件中包含一個表單，表單中則包含一個文字輸入元素。</p>
</td>
</tr>
<tr>
<td><img src="http://i19.photobucket.com/albums/b152/skyfate/blog/id0337-OM-4.png" alt="" /></td>
<td>
<p>然後我們在此標記語言文件當中再添加一個按鈕元素，然後重新載入該文件，因為文字輸入元素和按鈕元素是屬於同一層次的，所以在物件模型中的表現則是被包含在表單元素中。</p>
</td>
</tr>
</table>
<h2>物件的引用(The Object References)</h2>
<p>每當一份標記語言文件載入瀏覽器後，該文件中的所有物件均會依據瀏覽器文件物件模型(Document Object Model，DOM)所規定的層次結構來存放這些物件。假如一個腳本(Script)要控制一個物件，就會有一個相對應的方式來指向一個物件，我們讓腳本來指出它所要的是那一個物件，這種方式我們則可稱之為「物件的引用」或「物件的參考」。</p>
<p>對於每個物件的引用，其引用的路徑(層次)必須包括從頂端的層次到目標物件的層次，而在這個路徑中，每個相連物件之間必須使用句號來隔開彼此。</p>
<p>以下就是一個簡易的 JS 物件引用的示範：</p>
<table>
<tr>
<td><img src="http://i19.photobucket.com/albums/b152/skyfate/blog/id0337-OM-5.png" alt="" /></td>
<td>
<p>因為在每個視窗(或框架)中只能有一個文件，所以我們對於一個文件的引用方式為：</p>
<p><code class="MyCode0">window.document</code></p>
</td>
</tr>
<tr>
<td><img src="http://i19.photobucket.com/albums/b152/skyfate/blog/id0337-OM-6.png" alt="" /></td>
<td>　</td>
</tr>
<tr>
<td><img src="http://i19.photobucket.com/albums/b152/skyfate/blog/id0337-OM-7.png" alt="" /></td>
<td>　</td>
</tr>
<tr>
<td><img src="http://i19.photobucket.com/albums/b152/skyfate/blog/id0337-OM-8.png" alt="" /></td>
<td>　</td>
</tr>
</table>
<p>從上面我們可以瞭解到，我們引用一個層次越低的物件，其引用的路徑(層次)也就會越長。</p>
<p class="MyColor1">※因為在正常的情況下每一個視窗它就只能有一份文件，所以我們對於物件的引用上可以省略 window 這個物件，然後直接從 document 開始引用。</p>
<h2>屬性、方法與事件處理(Properties、Methods And Event Handlers)</h2>
<p>每個物件在某種程度上是唯一的，而我們定義一個物件可以從三方面來探討：它看起來像什麼、它是如何起作用的以及腳本如何控制它，這三方面分別代表了物件的<b>屬性</b>、<b>方法</b>及<b>事件處理</b>。</p>
<h3>屬性(Properties)</h3>
<p>每個物理上的物件都會有一些定義該物件特徵的集合，例如一支筆有其長度、直徑或者顏色等等的特徵，而我們可以把這些特徵統稱為屬性。每個特徵或多或少都有一個與它相對應的值，例如一支筆的顏色屬性可能是「黑色」、「藍色」或者「紅色」，這些顏色名稱就是一個文字值；而其長度對它而言就可能是一個數值。</p>
<p>例如：我們先前所提到那個王小明的說明：</p>
<p><code class="MyCode0">學校.3年3班.學生.王小明.<b class="MyColor1">身高</b></code></p>
<p>由此我們就可以知道，屬性是從屬於物件的，所以我們對於一個物件的屬性引用就是「物件引用」之後加一個句點，然後再加上屬性名稱。例如：</p>
<p><code class="MyCode0">document.bgcolor</code></p>
<h3>方法(Methods)</h3>
<p>屬性就像是描述物件的形容詞，而方法就像是物件的動詞，方法指的是所有和物件相關的動作。方法是瀏覽器的一些預設程序，例如 open() 、write() …等等。我們在網頁中安插一些 JS 的方法名稱，瀏覽器就會進行這些方法的程序。</p>
<p>讓方法有其作用的行為，我們可以稱之為「方法的引用」。在 JS 中，方法的引用是由一些方法名稱的<b class="MyColor1">關鍵字</b>加上括號 <b class="MyColor1">()</b> 所組合成的。例如：</p>
<p><code class="MyCode0">document.write()</code></p>
<p>有時候為了完成一些 Script 的任務，我們必須在過程中加入一些訊息，對於方法所發送或傳遞的訊息或者是讓其有作用的東西，我們可稱之為「參數」。例如：</p>
<p><code class="MyCode0">document.write(&quot;嗨～您好。&quot;)</code></p>
<p>上述簡短的範例則會在文件上顯示——嗨～您好。</p>
<h3>事件處理(Event Handlers)</h3>
<p>所謂事件指的是在文件中的動作。一般而言，通常指的是瀏覽者動作之後的結果，譬如瀏覽者點擊一個按鈕或者是在文字區中輸入一些字元。例如：</p>
<p><code class="MyCode0">&lt;input type=&quot;button&quot; value=&quot;請按我&quot; <b class="MyColor1">onclick</b>=&quot;window.alert(&#39;嗨～您好。&#39;)&quot; /&gt;</code></p>
<p>上面是一個簡單的例子，它在網頁上會產生一個按鈕，按鈕上的文字為「請按我」，onclick 是一個事件，當按下按鈕後則會產生一個對話訊息方塊，其內容訊息為「嗨～您好。」。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.beyes.tw/337/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Core Object &#8211; 字串物件(String Object)</title>
		<link>http://blog.beyes.tw/336</link>
		<comments>http://blog.beyes.tw/336#comments</comments>
		<pubDate>Sat, 09 May 2009 06:33:47 +0000</pubDate>
		<dc:creator>skyfate</dc:creator>
				<category><![CDATA[JavaScript1.5]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://blog.beyes.tw/?p=336</guid>
		<description><![CDATA[字串物件(String Object)


String 物件
兼容性：IE3+、NN2+、Moz1+、Safari1+




屬性
constructor、length、prototype


方法
anchor、big、blink、bold、fixed、fontcolor、fontsize、italics、link、small、strike、sub、sup、charAt、charCodeAt、concat、fromCharCode、indexOf、lastIndexOf、localeCompare、match、replace、search、slice、split、substr、substring、toLowerCase、toUpperCase、toString、valueOf



strObj = new String(Str)  Str：字串
String(Num)               Num：數值(轉換為字串)

用來存放或處理字串的物件。


//範例
var myStr = new String(12345);
var myData = myStr + 6789;			//自動將 6789 由數值轉換為字串
									//(&#34;字串 + 數值&#34;會自動變成&#34;字串 + 字串&#34;)

document.write(myData);				//結果為 123456789(這個是字串不是數值)
document.write(typeof(myData));		//檢查 myData 變數的結果為 string



big bold fixed italics small strike sub sup 方法
兼容性：IE3+、NN2+、Moz1+、Safari1+


blink 方法
兼容性：NN2+、Moz1+、Safari1+




strObj.big()
放大文字
strObj.blink()
閃爍文字
strObj.bold()
粗體文字


strObj.fixed()
等寬文字
strObj.italics()
斜體文字
strObj.small()
縮小文字


strObj.strike()
加刪除線
strObj.sub()
文字下標
strObj.sup()
文字上標


用來做文字上的修飾效果。

//範例
var myStr1 = &#34;The Time [...]]]></description>
			<content:encoded><![CDATA[<h2>字串物件(String Object)</h2>
<table class="js-tab">
<tr>
<td><span class="jsKeyWord">String</span> <span class="jsType">物件</span></td>
<td class="jsComp">兼容性：IE3+、NN2+、Moz1+、Safari1+</td>
</tr>
</table>
<table class="LN-tab">
<tr>
<td class="LN-BG1noWrap">屬性</td>
<td class="LN-BG1">constructor、length、prototype</td>
</tr>
<tr>
<td class="LN-BG1">方法</td>
<td class="LN-BG1">anchor、big、blink、bold、fixed、fontcolor、fontsize、italics、link、small、strike、sub、sup、charAt、<br />charCodeAt、concat、fromCharCode、indexOf、lastIndexOf、localeCompare、match、replace、search、<br />slice、split、substr、substring、toLowerCase、toUpperCase、toString、valueOf</td>
</tr>
</table>
<pre class="jsSyntax3">
strObj = new String(Str)  <span class="FontNormal">Str：字串</span>
String(Num)               <span class="FontNormal">Num：數值(轉換為字串)</span>
</pre>
<p>用來存放或處理字串的物件。</p>
<div style="clear: both;"></div>
<pre class="brush: jscript;">
//範例
var myStr = new String(12345);
var myData = myStr + 6789;			//自動將 6789 由數值轉換為字串
									//(&quot;字串 + 數值&quot;會自動變成&quot;字串 + 字串&quot;)

document.write(myData);				//結果為 123456789(這個是字串不是數值)
document.write(typeof(myData));		//檢查 myData 變數的結果為 string
</pre>
<table class="js-tab">
<tr>
<td><span class="jsKeyWord">big</span> <span class="jsKeyWord">bold</span> <span class="jsKeyWord">fixed</span> <span class="jsKeyWord">italics</span> <span class="jsKeyWord">small</span> <span class="jsKeyWord">strike</span> <span class="jsKeyWord">sub</span> <span class="jsKeyWord">sup</span> <span class="jsType">方法</span></td>
<td class="jsComp">兼容性：IE3+、NN2+、Moz1+、Safari1+</td>
</tr>
<tr>
<td><span class="jsKeyWord">blink</span> <span class="jsType">方法</span></td>
<td class="jsComp">兼容性：NN2+、Moz1+、Safari1+</td>
</tr>
</table>
<table class="LN-tab">
<tr>
<td class="LN-BG1"><b>strObj.big()</b></td>
<td class="LN-BG1">放大文字</td>
<td class="LN-BG1"><b>strObj.blink()</b></td>
<td class="LN-BG1">閃爍文字</td>
<td class="LN-BG1"><b>strObj.bold()</b></td>
<td class="LN-BG1">粗體文字</td>
</tr>
<tr>
<td class="LN-BG1"><b>strObj.fixed()</b></td>
<td class="LN-BG1">等寬文字</td>
<td class="LN-BG1"><b>strObj.italics()</b></td>
<td class="LN-BG1">斜體文字</td>
<td class="LN-BG1"><b>strObj.small()</b></td>
<td class="LN-BG1">縮小文字</td>
</tr>
<tr>
<td class="LN-BG1"><b>strObj.strike()</b></td>
<td class="LN-BG1">加刪除線</td>
<td class="LN-BG1"><b>strObj.sub()</b></td>
<td class="LN-BG1">文字下標</td>
<td class="LN-BG1"><b>strObj.sup()</b></td>
<td class="LN-BG1">文字上標</td>
</tr>
</table>
<p>用來做文字上的修飾效果。</p>
<pre class="brush: jscript;">
//範例
var myStr1 = &quot;The Time Of My Life&quot;;
var myStr2 = &quot;2&quot;;var myStr3 = &quot;3&quot;;
	document.write(myStr1.big() + &quot;&lt;br /&gt;&quot;);
	document.write(myStr1.blink() + &quot;&lt;br /&gt;&quot;);			//IE無效果
	document.write(myStr1.bold() + &quot;&lt;br /&gt;&quot;);
	document.write(myStr1.fixed() + &quot;&lt;br /&gt;&quot;);
	document.write(myStr1.italics() + &quot;&lt;br /&gt;&quot;);
	document.write(myStr1.small() + &quot;&lt;br /&gt;&quot;);
	document.write(myStr1.strike() + &quot;&lt;br /&gt;&quot;);
	document.write(&quot;H&quot; + myStr2.sub() + &quot;O&quot; + &quot;&lt;br /&gt;&quot;);
	document.write(&quot;2&quot; + myStr3.sup() + &quot;= 8&quot;);
</pre>
<div class="alignright">
<input type="button" value="上述範例結果" onclick="window.open('http://boxbeyes.googlepages.com/JS-StrObject01.html','','menubar=no,status=yes,scrollbars=yes,top=0,left=0,toolbar=no,width=640,height=480')" />
</div>
<table class="js-tab">
<tr>
<td><span class="jsKeyWord">fontcolor</span> <span class="jsKeyWord">fontsize</span> <span class="jsType">方法</span></td>
<td class="jsComp">兼容性：IE3+、NN2+、Moz1+、Safari1+</td>
</tr>
</table>
<pre class="jsSyntax3">
strObj.fontcolor(color)  <span class="FontNormal">color：顏色的名稱或 RGB</span>
strObj.fontsize(size)    <span class="FontNormal">size：1~7 的文字大小</span>
</pre>
<p>用來設定文字的顏色與大小。</p>
<div style="clear: both;"></div>
<pre class="brush: jscript;">
//範例
var myStr = &quot;The Time Of My Life&quot;;
	document.write(myStr.fontcolor(&quot;red&quot;) + &quot;&lt;br /&gt;&quot;);
	document.write(myStr.fontsize(6) + &quot;&lt;br /&gt;&quot;);
	document.write(myStr.fontcolor(&quot;blue&quot;).fontsize(7));
</pre>
<div class="alignright">
<input type="button" value="上述範例結果" onclick="window.open('http://boxbeyes.googlepages.com/JS-StrObject02.html','','menubar=no,status=yes,scrollbars=yes,top=0,left=0,toolbar=no,width=640,height=480')" />
</div>
<table class="js-tab">
<tr>
<td><span class="jsKeyWord">link</span> <span class="jsType">方法</span></td>
<td class="jsComp">兼容性：IE3+、NN2+、Moz1+、Safari1+</td>
</tr>
</table>
<pre class="jsSyntax3">
strObj.link(url)  <span class="FontNormal">url：鏈結的網址或路徑與檔名</span>
</pre>
<p>用來設定文字的超鏈結。</p>
<div style="clear: both;"></div>
<pre class="brush: jscript;">
//範例
var myStr = &quot;TO SeedNET&quot;;
	document.write(myStr.link('http://www.seed.net.tw'));
</pre>
<div class="alignright">
<input type="button" value="上述範例結果" onclick="window.open('http://boxbeyes.googlepages.com/JS-StrObject03.html','','menubar=no,status=yes,scrollbars=yes,top=0,left=0,toolbar=no,width=640,height=480')" />
</div>
<table class="js-tab">
<tr>
<td><span class="jsKeyWord">toLowerCase</span> <span class="jsKeyWord">toUpperCase</span> <span class="jsType">方法</span></td>
<td class="jsComp">兼容性：IE3+、NN2+、Moz1+、Safari1+</td>
</tr>
</table>
<pre class="jsSyntax3">
strObj.toLowerCase()  <span class="FontNormal">全部轉為小寫</span>
strObj.toUpperCase()  <span class="FontNormal">全部轉為大寫</span>
</pre>
<p>用來設定英文字母大小寫的轉換。</p>
<div style="clear: both;"></div>
<pre class="brush: jscript;">
//範例
var myStr = &quot;AChGUydseEZxQbj&quot;;
	document.write(&quot;原本字串內容：&quot; + myStr + &quot;&lt;br /&gt;&quot;);
	document.write(&quot;全部轉為小寫：&quot; + myStr.toLowerCase() + &quot;&lt;br /&gt;&quot;);
	document.write(&quot;全部轉為大寫：&quot; + myStr.toUpperCase());
</pre>
<div class="alignright">
<input type="button" value="上述範例結果" onclick="window.open('http://boxbeyes.googlepages.com/JS-StrObject04.html','','menubar=no,status=yes,scrollbars=yes,top=0,left=0,toolbar=no,width=640,height=480')" />
</div>
<table class="js-tab">
<tr>
<td><span class="jsKeyWord">charCodeAt</span> <span class="jsKeyWord">fromCharCode</span> <span class="jsType">方法</span></td>
<td class="jsComp">兼容性：IE4+、NN4+、Moz1+、Safari1+</td>
</tr>
</table>
<pre class="jsSyntax3">
strObj.charCodeAt(n)              <span class="FontNormal">將字元轉換成 Unicode 編碼</span>
                                  <span class="FontNormal">n：指定字元的位置</span>
String.fromCharCode(c1,c2,…,cN)   <span class="FontNormal">將 Unicode 編碼轉換成字元</span>
                                  <span class="FontNormal">c1,c2,…,cN：編碼</span>
</pre>
<p>用來做字碼與字元的轉換。</p>
<div style="clear: both;"></div>
<pre class="brush: jscript;">
//範例
//指定字元的位置其起始位置為 0 例如位置 3 表示字串中的第 4 個字元
var myStr = &quot;JavaScript&quot;;
	document.write(myStr.charCodeAt(4));		//結果 S 轉換成 83
	document.write(String.fromCharCode(83));	//結果 83 轉換成 S
</pre>
<table class="js-tab">
<tr>
<td><span class="jsKeyWord">length</span> <span class="jsType">屬性</span></td>
<td class="jsComp">兼容性：IE3+、NN2+、Moz1+、Safari1+</td>
</tr>
</table>
<pre class="jsSyntax3">
strObj.length
</pre>
<p>用來傳回字串的長度。</p>
<div style="clear: both;"></div>
<pre class="brush: jscript;">
//範例
document.write(&quot;webpages&quot;.length);				//結果為 8
document.write(&quot;家庭聯絡簿&quot;.length);				//結果為 5
</pre>
<table class="js-tab">
<tr>
<td><span class="jsKeyWord">indexOf</span> <span class="jsKeyWord">lastIndexOf</span> <span class="jsType">方法</span></td>
<td class="jsComp">兼容性：IE4+、NN4+、Moz1+、Safari1+</td>
</tr>
</table>
<pre class="jsSyntax3">
strObj.indexOf(subStr,start)      <span class="FontNormal">subStr：欲搜尋的子字串</span>
strObj.lastIndexOf(subStr,start)  <span class="FontNormal">start：開始搜尋的位置</span>
</pre>
<p>indexOf 會從字串左側開始搜尋，lastIndexOf 是會從字串右側開始搜尋。</p>
<div style="clear: both;"></div>
<pre class="brush: jscript;">
//範例
&lt;div&gt;ABCDEFGhijklmnopq&lt;/div&gt;					//網頁中某段內容

//網頁中 JavaScript 區段
document.write(&quot;ABCDEFG&quot;.indexOf(&quot;FG&quot;,0));		//傳回 FG 在字串中 5 的起始位置上
document.write(&quot;ABCDEFG&quot;.lastIndexOf(&quot;EF&quot;,10));	//傳回在 4 的位置上
document.write(&quot;ABCDEFG&quot;.indexOf(&quot;123&quot;,0));		//找不到傳回 -1
</pre>
<table class="js-tab">
<tr>
<td><span class="jsKeyWord">match</span> <span class="jsKeyWord">replace</span> <span class="jsKeyWord">search</span> <span class="jsType">方法</span></td>
<td class="jsComp">兼容性：IE4+、NN4+、Moz1+、Safari1+</td>
</tr>
</table>
<pre class="jsSyntax3">
strObj.match(regExp)           <span class="FontNormal">strObj：字串或字串物件的名稱</span>
strObj.replace(regExp,repStr)  <span class="FontNormal">regExp：正規表達式的樣式字串</span>
strObj.search(regExp)          <span class="FontNormal">repStr：取代用的字串</span>
</pre>
<p>使用正規表達式來比對(match)、取代(replace)與搜尋(search)特定的字串。無法比對成功時傳回 null。</p>
<div style="clear: both;"></div>
<pre class="brush: jscript;">
//範例
var myStr = &quot;abcdef ABCDEF&quot;
	document.write(myStr.match(/abc/gi));		//比對結果顯示為 abc,ABC
	document.write(myStr.search(/ABC/));		//搜尋結果在字串 7 的位置找到
	document.write(myStr.replace(/ABC/,123));	//結果顯示為 abcdef 123DEF
</pre>
<table class="js-tab">
<tr>
<td><span class="jsKeyWord">charAt</span> <span class="jsType">方法</span></td>
<td class="jsComp">兼容性：IE3+、NN2+、Moz1+、Safari1+</td>
</tr>
<tr>
<td><span class="jsKeyWord">slice</span> <span class="jsType">方法</span></td>
<td class="jsComp">兼容性：IE4+、NN4+、Moz1+、Safari1+</td>
</tr>
<tr>
<td><span class="jsKeyWord">split</span> <span class="jsType">方法</span></td>
<td class="jsComp">兼容性：IE4+、NN3+、Moz1+、Safari1+</td>
</tr>
<tr>
<td><span class="jsKeyWord">substr</span> <span class="jsType">方法</span></td>
<td class="jsComp">兼容性：IE4+、NN4+、Moz1+、Safari1+</td>
</tr>
<tr>
<td><span class="jsKeyWord">substring</span> <span class="jsType">方法</span></td>
<td class="jsComp">兼容性：IE3+、NN2+、Moz1+、Safari1+</td>
</tr>
</table>
<pre class="jsSyntax3">
strObj.charAt(N)               <span class="FontNormal">strObj：字串或字串物件的名稱</span>
strObj.slice(start,end)        <span class="FontNormal">N：字元位置</span>
strObj.split(delimiter,limit)  <span class="FontNormal">start：字元起始位置</span>
strObj.substr(start,strcount)  <span class="FontNormal">end：字元終止位置</span>
strObj.substring(start,end)    <span class="FontNormal">delimiter：切割用的字串</span>
                               <span class="FontNormal">limit：最大分割數</span>
                               <span class="FontNormal">strcount：字元數</span>
</pre>
<p>charAt 是用來取出一個字元，slice 是用來取出字串片段，split 是以指定的字串來分割，substr 是取出指定位置起始的字串，substring 是取出指定範圍內的字串。</p>
<div style="clear: both;"></div>
<pre class="brush: jscript;">
//範例
document.write(&quot;ABCDEFGHI&quot;.charAt(5));		//結果為 F(字串起始的位置是 0)
document.write(&quot;ABCDEFGHI&quot;.slice(3,6));		//結果為 DEF(起始於 3 終止於 6)
document.write(&quot;ABCDEFGHI&quot;.split(&quot;FGHI&quot;));	//結果為 ABCDE,
document.write(&quot;ABCDEFGHI&quot;.substr(0,3));	//結果為 ABC
document.write(&quot;ABCDEFGHI&quot;.substring(1,6));	//結果為 BCDEF
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.beyes.tw/336/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Core Object &#8211; 正規表達式物件(RegExp Object)</title>
		<link>http://blog.beyes.tw/335</link>
		<comments>http://blog.beyes.tw/335#comments</comments>
		<pubDate>Fri, 08 May 2009 07:39:39 +0000</pubDate>
		<dc:creator>skyfate</dc:creator>
				<category><![CDATA[JavaScript1.5]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://blog.beyes.tw/?p=335</guid>
		<description><![CDATA[正規表達式物件(RegExp Object)


RegExp 物件
兼容性：IE4+、NN4+、Moz1+、Safari1+




屬性
constructor、global、ignoreCase、lastIndex、multilane、source、$1~$9、lastMatch、leftContext、rightContext


方法
exec、test



regObj = new RegExp(pattern, attributes)     regObj：正規表達式物件的名稱
                                            [...]]]></description>
			<content:encoded><![CDATA[<h2>正規表達式物件(RegExp Object)</h2>
<table class="js-tab">
<tr>
<td><span class="jsKeyWord">RegExp</span> <span class="jsType">物件</span></td>
<td class="jsComp">兼容性：IE4+、NN4+、Moz1+、Safari1+</td>
</tr>
</table>
<table class="myTab">
<tr>
<td class="LN-BG1noWrap">屬性</td>
<td class="LN-BG1">constructor、global、ignoreCase、lastIndex、multilane、source、$1~$9、<br />lastMatch、leftContext、rightContext</td>
</tr>
<tr>
<td class="LN-BG1">方法</td>
<td class="LN-BG1">exec、test</td>
</tr>
</table>
<pre class="jsSyntax1">
regObj = new RegExp(pattern, attributes)     <span class="FontNormal">regObj：正規表達式物件的名稱</span>
                                             <span class="FontNormal">pattern：字串樣式</span>
regObj = /pattern/attributes                 <span class="FontNormal">attributes：欲比對的方式</span>
                                             <span class="FontNormal">g = 完全比對、i = 忽略大小寫、m = 多列。</span>
                                             <span class="FontNormal">比對無法成功時傳回 null。</span>
</pre>
<p>用來存放正規表達式的物件。</p>
<div style="clear: both;"></div>
<pre class="brush: jscript;">
//範例
var myReg = new RegExp(&quot;ABc&quot;, &quot;ig&quot;);	//以 ABc 樣式字串來比對並且忽略大小寫與完全比對
var myStr = &quot;abcdef ABCDEF&quot;;			//欲比對的目標字串
var myData = myStr.match(myReg);		//使用字串物件的 match() 來比對

document.write(&quot;字串 ABc 比對&quot; + myStr + &quot;的結果為：&quot; + myData);
//結果為 字串 ABc 比對abcdef ABCDEF的結果為：abc,ABC
</pre>
<table class="js-tab">
<tr>
<td><span class="jsKeyWord">$1 ~ $9</span> <span class="jsType">屬性</span></td>
<td class="jsComp">兼容性：IE4+、NN4+、Moz1+、Safari1+</td>
</tr>
</table>
<pre class="jsSyntax3">RegExp.$1~$9

<span class="FontNormal">$1 ~ $9：$1 是第一個字串 $2 是第二個字串…以此類推</span></pre>
<p>$1~$9 屬性是用來存放與樣式比對成功的字串。</p>
<div style="clear: both;"></div>
<pre class="brush: jscript;">
//範例
//樣式為 &quot;任意的英數字元 + 空白字元 + 任意的英數字元 + 空白字元 + 任意的英數字元&quot; 且為完全比對
//並且使用括號來保存所匹配的英數字元
var myReg = new RegExp(&quot;(\\w+)\\s(\\w+)\\s(\\w+)&quot;,&quot;g&quot;);
var myStr = &quot;abc ABC 123&quot;;								//欲比對的目標
var myData = myStr.replace(myReg,&quot;$3, $1 and $2&quot;);		//使用字串物件的 replace() 來置換

document.write(&quot;myData 內容： &quot; + myData);              //myData 內容： 123, abc and ABC
document.write(&quot;$1 內容結果： &quot; + RegExp.$1);			//$1 內容結果： abc
document.write(&quot;$2 內容結果： &quot; + RegExp.$2);			//$2 內容結果： ABC
document.write(&quot;$3 內容結果： &quot; + RegExp.$3);			//$3 內容結果： 123
</pre>
<table class="js-tab">
<tr>
<td><span class="jsKeyWord">exec</span> <span class="jsKeyWord">test</span> <span class="jsType">方法</span> <span class="jsKeyWord">lastIndex</span> <span class="jsType">屬性</span></td>
<td class="jsComp">兼容性：IE4+、NN4+、Moz1+、Safari1+</td>
</tr>
</table>
<pre class="jsSyntax3">
regObj.exec(string)         <span class="FontNormal">regObj：正規表達式物件的名稱</span>
regObj.lastIndex = start    <span class="FontNormal">string：欲搜尋的字串</span>
regObj.test(string)         <span class="FontNormal">start：開始位置</span>
</pre>
<p>exec 是用來開始比對。</p>
<p>test 是用來檢查有無匹配成功。</p>
<p>lastIndex 是用來指定<b>下次比對</b>的起始位置，如果沒有使用 <b class="MyColor1">g</b> 來完全比對或者不代表是全域樣式的 RegExp 物件，將不能使用這個屬性。</p>
<pre class="brush: jscript;">
//範例
myReg = /..ABC/g;								//樣式為 &quot;任何字元 + 任何字元 + ABC&quot;
myReg.lastIndex = 5;							//從目標的第 6 個字元開始比對
myFlag = myReg.test(&quot;11ABC22ABC33ABCXXABC&quot;);	//檢查有無匹配樣式的字串
myData = myReg.exec(&quot;11ABC22ABC33ABCXXABC&quot;);	//開始比對目標字串

document.write(&quot;test 的結果： &quot; + myFlag);		//結果為 true
document.write(&quot;exec 的結果： &quot; + myData);		//結果為 33ABC
</pre>
<table class="js-tab">
<tr>
<td><span class="jsKeyWord">global</span> <span class="jsKeyWord">ignoreCase</span> <span class="jsType">屬性</span></td>
<td class="jsComp">兼容性：IE5.5+、NN4+、Moz1+、Safari1+</td>
</tr>
<tr>
<td><span class="jsKeyWord">multiline</span> <span class="jsType">屬性</span></td>
<td class="jsComp">兼容性：IE5.5+、NN6+、Moz1+、Safari1+</td>
</tr>
</table>
<pre class="jsSyntax3">
regObj.global         <span class="FontNormal">傳回是否為完全比對的值(true或false) - <b>g</b></span>
regObj.ignoreCase     <span class="FontNormal">傳回是否為忽略大小寫的值(true或false) - <b>i</b></span>
RegExp.multiline      <span class="FontNormal">指定是否要多列比對的值(true或false) - <b>m</b></span>
</pre>
<p>用來傳回或指定搜尋條件的 true 或 false 值。</p>
<div style="clear: both;"></div>
<pre class="brush: jscript;">
//範例
var myReg1 = /ABC/;
var myReg2 = /ABC/g;
var myReg3 = /ABC/;
var myReg4 = /ABC/i;

document.write(myReg1.global);			//結果為 false
document.write(myReg2.global);			//結果為 true
document.write(myReg3.ignoreCase);		//結果為 false
document.write(myReg4.ignoreCase);		//結果為 true
</pre>
<table class="js-tab">
<tr>
<td><span class="jsKeyWord">lastMatch</span> <span class="jsType">屬性</span></td>
<td class="jsComp">兼容性：IE5.5+、NN4+、Moz1+、Safari1+</td>
</tr>
</table>
<pre class="jsSyntax3">
RegExp.lastMatch         <span class="FontNormal">傳回最後匹配成功的字串</span>
</pre>
<p>用來傳回最後比對成功的字串。</p>
<div style="clear: both;"></div>
<pre class="brush: jscript;">
//範例
myReg = /ABC../g;
myStr = &quot;ABC12ABC34ABC56ABC789&quot;;
myData = myStr.match(myReg);

document.write(&quot;全部比對成功的字串： &quot; + myData);
//結果為 ABC12,ABC34,ABC56,ABC78
document.write(&quot;最後比對成功的字串： &quot; + RegExp.lastMatch);
//結果為 ABC78
</pre>
<table class="js-tab">
<tr>
<td><span class="jsKeyWord">leftContext</span> <span class="jsKeyWord">rightContext</span> <span class="jsType">屬性</span></td>
<td class="jsComp">兼容性：IE5.5+、NN4+、Moz1+、Safari1+</td>
</tr>
</table>
<pre class="jsSyntax3">
RegExp.leftContext         <span class="FontNormal">傳回匹配成功字串的左邊字串</span>
RegExp.rightContext        <span class="FontNormal">傳回匹配成功字串的右邊字串</span>
</pre>
<p>用來傳回所匹配字串的左右邊字串。</p>
<div style="clear: both;"></div>
<pre class="brush: jscript;">
//範例
myReg = new RegExp(&quot;(i am)&quot;);
myStr = &quot;hi~i am skyfate.&quot;;
myData = myStr.match(myReg);

document.write(&quot;比對的字串： &quot; + myStr);					//結果為 hi~i am skyfate.
document.write(&quot;比對的結果： &quot; + myData);					//結果為 i am,i am
document.write(&quot;左方的字串： &quot; + RegExp.leftContext);		//結果為 hi~
document.write(&quot;右方的字串： &quot; + RegExp.rightContext);	//結果為 skyfate.
</pre>
<table class="js-tab">
<tr>
<td><span class="jsKeyWord">source</span> <span class="jsType">屬性</span></td>
<td class="jsComp">兼容性：IE4+、NN4+、Moz1+、Safari1+</td>
</tr>
</table>
<pre class="jsSyntax3">regObj.source</pre>
<p>用來傳回樣式的字串。</p>
<div style="clear: both;"></div>
<pre class="brush: jscript;">
//範例
var myReg = new RegExp(&quot;The Time of my Life&quot;, &quot;ig&quot;);

document.write(&quot;正規表達式物件的樣式字串：&quot; + myReg.source);
//正規表達式物件的樣式字串：The Time of my Life
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.beyes.tw/335/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Core Object &#8211; 數值物件(Number Object)</title>
		<link>http://blog.beyes.tw/334</link>
		<comments>http://blog.beyes.tw/334#comments</comments>
		<pubDate>Thu, 07 May 2009 07:52:52 +0000</pubDate>
		<dc:creator>skyfate</dc:creator>
				<category><![CDATA[JavaScript1.5]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://blog.beyes.tw/?p=334</guid>
		<description><![CDATA[數值物件(Number Object)


Number 物件
兼容性：IE4+、NN3+、Moz1+、Safari1+


numObj = new Number()
numObj = new Number(value)

numObj：數值物件的名稱
value：數值



屬性
constructor、prototype、MAX_VALUE、MIN_VALUE、NaN、NEGATIVE_INFINITY、POSITIVE_INFINITY


方法
toExponential、toFixed、toLocaleString、toString、toPrecision、valueOf


這是用來存取數值的物件，其主要用途是想將變數儲存為數值。


//範例
var a = &#34;99&#34;;								//字串
var b = new Number(a);
document.write(a + 1);						//結果為 991
document.write(b + 1);						//結果為 100



MAX_VALUE MIN_VALUE NEGATIVE_INFINITY POSITIVE_INFINITY NaN 屬性
兼容性：IE4+、NN3+、Moz1+、Safari1+



numObj.MAX_VALUE             傳回可使用的最大值
numObj.MIN_VALUE             傳回可使用的最小值
numObj.NEGATIVE_INFINITY   [...]]]></description>
			<content:encoded><![CDATA[<h2>數值物件(Number Object)</h2>
<table class="js-tab">
<tr>
<td><span class="jsKeyWord">Number</span> <span class="jsType">物件</span></td>
<td class="jsComp">兼容性：IE4+、NN3+、Moz1+、Safari1+</td>
</tr>
</table>
<pre class="jsSyntax3">numObj = new Number()
numObj = new Number(value)
<p class="FontNormal">
numObj：數值物件的名稱
value：數值
</pre>
<table class="myTab">
<tr>
<td class="LN-BG1noWrap">屬性</td>
<td class="LN-BG1">constructor、prototype、MAX_VALUE、MIN_VALUE、NaN、<br />NEGATIVE_INFINITY、POSITIVE_INFINITY</td>
</tr>
<tr>
<td class="LN-BG1">方法</td>
<td class="LN-BG1">toExponential、toFixed、toLocaleString、toString、<br />toPrecision、valueOf</td>
</tr>
</table>
<p>這是用來存取數值的物件，其主要用途是想將變數儲存為數值。</p>
<div style="clear: both;"></div>
<pre class="brush: jscript;">
//範例
var a = &quot;99&quot;;								//字串
var b = new Number(a);
document.write(a + 1);						//結果為 991
document.write(b + 1);						//結果為 100
</pre>
<table class="js-tab">
<tr>
<td><span class="jsKeyWord">MAX_VALUE</span> <span class="jsKeyWord">MIN_VALUE</span> <br /><span class="jsKeyWord">NEGATIVE_INFINITY</span> <span class="jsKeyWord">POSITIVE_INFINITY</span> <br /><span class="jsKeyWord">NaN</span> <span class="jsType">屬性</span></td>
<td class="jsComp">兼容性：IE4+、NN3+、Moz1+、Safari1+</td>
</tr>
</table>
<pre class="jsSyntax2">
numObj.MAX_VALUE             <span class="FontNormal">傳回可使用的最大值</span>
numObj.MIN_VALUE             <span class="FontNormal">傳回可使用的最小值</span>
numObj.NEGATIVE_INFINITY     <span class="FontNormal">傳回負的無限大</span>
numObj.POSITIVE_INFINITY     <span class="FontNormal">傳回無限大</span>
numObj.NaN                   <span class="FontNormal">傳回非數值</span>
</pre>
<p>這是用來存放數值的最大、最小值或者無限大的值。NaN(Not a Number)，非數值。</p>
<div style="clear: both;"></div>
<pre class="brush: jscript;">
//範例
document.write(Number.MAX_VALUE);			//結果為 1.7976931348623157e+308
document.write(Number.MIN_VALUE);			//結果為 5e-324
document.write(Number.NEGATIVE_INFINITY);	//結果為 -Infinity
document.write(Number.POSITIVE_INFINITY);	//結果為 Infinity
document.write(Number.NaN);					//結果為 NaN
</pre>
<table class="js-tab">
<tr>
<td><span class="jsKeyWord">toExponential</span> <span class="jsKeyWord">toFixed</span> <span class="jsKeyWord">toPrecision</span> <span class="jsType">屬性</span></td>
<td class="jsComp">兼容性：IE4+、NN3+、Moz1+、Safari1+</td>
</tr>
</table>
<table class="LN-tab3">
<tr>
<td class="LN-BG1">Value.toExponential(v1)</td>
<td class="LN-BG1" rowspan="3">Value：欲轉換的值<br />v1：小數部分欲顯示的位數(個位數.v1e+指數)<br />v2：欲指定的小數位數<br />v3：欲指定的位數</td>
</tr>
<tr>
<td class="LN-BG1">Value.toFixed(v2)</td>
</tr>
<tr>
<td class="LN-BG1">Value.toPrecision(v3)</td>
</tr>
</table>
<p>toExponential 是用來傳回轉換成指數形式的值，toFixed 是用來傳回指定小數位數的值，toPrecision 是用來傳回指定位數的值。</p>
<div style="clear: both;"></div>
<pre class="brush: jscript;">
//範例
var a = 345600.999;
	document.write(a.toExponential());		//結果為 3.45600999e+5
	document.write(a.toExponential(5));		//結果為 3.45601e+5
	document.write(a.toFixed(2));			//結果為 345601.00
	document.write(a.toPrecision(12));		//結果為 345600.999000
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.beyes.tw/334/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Core Object &#8211; 數學函式物件(Math Object)</title>
		<link>http://blog.beyes.tw/333</link>
		<comments>http://blog.beyes.tw/333#comments</comments>
		<pubDate>Thu, 07 May 2009 04:18:17 +0000</pubDate>
		<dc:creator>skyfate</dc:creator>
				<category><![CDATA[JavaScript1.5]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://blog.beyes.tw/?p=333</guid>
		<description><![CDATA[數學函式物件(Math Object)


Math 物件
兼容性：IE3+、NN2+、Moz1+、Safari1+


Math.constant
Math.function()

constant：數學常數
function()：數學函式



屬性
E、LN2、LN10、LOG2E、LOG10E、PI、SQRT1_2、SQRT2


方法
abs、acos、asin、atan、atan2、ceil、cos、exp、floor、log、max、min、pow、random、round、sin、sqrt、tan


Math 物件擁有數學常數(屬性)和函式(方法)，想使用這個物件的話，並不需要使用 new 來建立物件的實體(副本)。


abs 方法
兼容性：IE3+、NN2+、Moz1+、Safari1+


PI 屬性
兼容性：IE3+、NN2+、Moz1+、Safari1+


Math.abs(x)
Math.PI

x：值或算數運算式
abs 是用來傳回絕對值。
PI 是用來傳回圓週率的值。


//範例
document.write(Math.abs(1 - 4));				//結果為 3
document.write(Math.PI);						//結果為 3.141592653589793



sin cos tan asin acos atan atan2 方法
兼容性：IE3+、NN2+、Moz1+、Safari1+




Math.sin(x)
傳回正弦值
Math.asin(x)
傳回反正弦值


Math.cos(x)
傳回餘弦值
Math.acos(x)
傳回反餘弦值


Math.tan(x)
傳回正切值
Math.atan(x)
傳回反正切值


Math.atan2(x,y)
從(x,y)座標傳回夾角
　
　


用來做三角函數的處理。


//範例
var a = 1;
	document.write(Math.sin(a));				//結果為 0.8414709848078965
	document.write(Math.cos(a));				//結果為 0.5403023058681398
	document.write(Math.tan(a));				//結果為 1.5574077246549023
	document.write(Math.asin(a));				//結果為 1.5707963267948966
	document.write(Math.acos(a));				//結果為 0
	document.write(Math.atan(a));				//結果為 0.7853981633974483
	document.write(Math.atan(a,1));				//結果為 0.7853981633974483



ceil floor round 方法
兼容性：IE3+、NN2+、Moz1+、Safari1+




Math.ceil(x)
傳回無條件進入後的結果


Math.floor(x)
傳回無條件捨去後的結果


Math.round(x)
傳回四捨五入後的結果


用來將小數整數化。


//範例
var a = 5.67;var b = 5.43;
	document.write(Math.ceil(a));				//結果為 6
	document.write(Math.floor(a));				//結果為 5
	document.write(Math.round(b));				//結果為 5



log 方法
兼容性：IE3+、NN2+、Moz1+、Safari1+


LOG2E LOG10E 屬性
兼容性：IE3+、NN2+、Moz1+、Safari1+


E LN2 LN10 屬性
兼容性：IE3+、NN2+、Moz1+、Safari1+




Math.log(x)
傳回自然對數的值
Math.E
傳回自然對數的底數 e(Euler常數)


Math.LOG2E
傳回以 2 為底數時 e 的對數值
Math.LN2
傳回 2 [...]]]></description>
			<content:encoded><![CDATA[<h2>數學函式物件(Math Object)</h2>
<table class="js-tab">
<tr>
<td><span class="jsKeyWord">Math</span> <span class="jsType">物件</span></td>
<td class="jsComp">兼容性：IE3+、NN2+、Moz1+、Safari1+</td>
</tr>
</table>
<pre class="jsSyntax3">Math.constant
Math.function()
<p class="FontNormal">
constant：數學常數
function()：數學函式
</pre>
<table class="myTab">
<tr>
<td class="LN-BG1noWrap">屬性</td>
<td class="LN-BG1">E、LN2、LN10、LOG2E、LOG10E、PI、SQRT1_2、SQRT2</td>
</tr>
<tr>
<td class="LN-BG1">方法</td>
<td class="LN-BG1">abs、acos、asin、atan、atan2、ceil、cos、exp、floor、log、max、min、pow、<br />random、round、sin、sqrt、tan</td>
</tr>
</table>
<p>Math 物件擁有數學常數(屬性)和函式(方法)，想使用這個物件的話，並不需要使用 <b>new</b> 來建立物件的實體(副本)。</p>
<table class="js-tab">
<tr>
<td><span class="jsKeyWord">abs</span> <span class="jsType">方法</span></td>
<td class="jsComp">兼容性：IE3+、NN2+、Moz1+、Safari1+</td>
</tr>
<tr>
<td><span class="jsKeyWord">PI</span> <span class="jsType">屬性</span></td>
<td class="jsComp">兼容性：IE3+、NN2+、Moz1+、Safari1+</td>
</tr>
</table>
<pre class="jsSyntax3">Math.abs(x)
Math.PI
<span class="FontNormal">
x：值或算數運算式</span></pre>
<p>abs 是用來傳回絕對值。</p>
<p>PI 是用來傳回圓週率的值。</p>
<div style="clear: both;"></div>
<pre class="brush: jscript;">
//範例
document.write(Math.abs(1 - 4));				//結果為 3
document.write(Math.PI);						//結果為 3.141592653589793
</pre>
<table class="js-tab">
<tr>
<td><span class="jsKeyWord">sin</span> <span class="jsKeyWord">cos</span> <span class="jsKeyWord">tan</span> <span class="jsKeyWord">asin</span> <span class="jsKeyWord">acos</span> <span class="jsKeyWord">atan</span> <span class="jsKeyWord">atan2</span> <span class="jsType">方法</span></td>
<td class="jsComp">兼容性：IE3+、NN2+、Moz1+、Safari1+</td>
</tr>
</table>
<table class="LN-tab3">
<tr>
<td class="LN-BG1">Math.sin(x)</td>
<td class="LN-BG1">傳回正弦值</td>
<td class="LN-BG1">Math.asin(x)</td>
<td class="LN-BG1">傳回反正弦值</td>
</tr>
<tr>
<td class="LN-BG1">Math.cos(x)</td>
<td class="LN-BG1">傳回餘弦值</td>
<td class="LN-BG1">Math.acos(x)</td>
<td class="LN-BG1">傳回反餘弦值</td>
</tr>
<tr>
<td class="LN-BG1">Math.tan(x)</td>
<td class="LN-BG1">傳回正切值</td>
<td class="LN-BG1">Math.atan(x)</td>
<td class="LN-BG1">傳回反正切值</td>
</tr>
<tr>
<td class="LN-BG1">Math.atan2(x,y)</td>
<td class="LN-BG1">從(x,y)座標傳回夾角</td>
<td class="LN-BG1">　</td>
<td class="LN-BG1">　</td>
</tr>
</table>
<p>用來做三角函數的處理。</p>
<div style="clear: both;"></div>
<pre class="brush: jscript;">
//範例
var a = 1;
	document.write(Math.sin(a));				//結果為 0.8414709848078965
	document.write(Math.cos(a));				//結果為 0.5403023058681398
	document.write(Math.tan(a));				//結果為 1.5574077246549023
	document.write(Math.asin(a));				//結果為 1.5707963267948966
	document.write(Math.acos(a));				//結果為 0
	document.write(Math.atan(a));				//結果為 0.7853981633974483
	document.write(Math.atan(a,1));				//結果為 0.7853981633974483
</pre>
<table class="js-tab">
<tr>
<td><span class="jsKeyWord">ceil</span> <span class="jsKeyWord">floor</span> <span class="jsKeyWord">round</span> <span class="jsType">方法</span></td>
<td class="jsComp">兼容性：IE3+、NN2+、Moz1+、Safari1+</td>
</tr>
</table>
<table class="LN-tab3">
<tr>
<td class="LN-BG1">Math.ceil(x)</td>
<td class="LN-BG1">傳回無條件進入後的結果</td>
</tr>
<tr>
<td class="LN-BG1">Math.floor(x)</td>
<td class="LN-BG1">傳回無條件捨去後的結果</td>
</tr>
<tr>
<td class="LN-BG1">Math.round(x)</td>
<td class="LN-BG1">傳回四捨五入後的結果</td>
</tr>
</table>
<p>用來將小數整數化。</p>
<div style="clear: both;"></div>
<pre class="brush: jscript;">
//範例
var a = 5.67;var b = 5.43;
	document.write(Math.ceil(a));				//結果為 6
	document.write(Math.floor(a));				//結果為 5
	document.write(Math.round(b));				//結果為 5
</pre>
<table class="js-tab">
<tr>
<td><span class="jsKeyWord">log</span> <span class="jsType">方法</span></td>
<td class="jsComp">兼容性：IE3+、NN2+、Moz1+、Safari1+</td>
</tr>
<tr>
<td><span class="jsKeyWord">LOG2E</span> <span class="jsKeyWord">LOG10E</span> <span class="jsType">屬性</span></td>
<td class="jsComp">兼容性：IE3+、NN2+、Moz1+、Safari1+</td>
</tr>
<tr>
<td><span class="jsKeyWord">E</span> <span class="jsKeyWord">LN2</span> <span class="jsKeyWord">LN10</span> <span class="jsType">屬性</span></td>
<td class="jsComp">兼容性：IE3+、NN2+、Moz1+、Safari1+</td>
</tr>
</table>
<table class="LN-tab">
<tr>
<td class="LN-BG1">Math.log(x)</td>
<td class="LN-BG1">傳回自然對數的值</td>
<td class="LN-BG1">Math.E</td>
<td class="LN-BG1">傳回自然對數的底數 e(Euler常數)</td>
</tr>
<tr>
<td class="LN-BG1">Math.LOG2E</td>
<td class="LN-BG1">傳回以 2 為底數時 e 的對數值</td>
<td class="LN-BG1">Math.LN2</td>
<td class="LN-BG1">傳回 2 的自然對數值</td>
</tr>
<tr>
<td class="LN-BG1">Math.LOG10E</td>
<td class="LN-BG1">傳回以 10 為底數時 e 的對數值</td>
<td class="LN-BG1">Math.LN10</td>
<td class="LN-BG1">傳回 10 的自然對數值</td>
</tr>
</table>
<p>log、LOG2E、LOG10E 是用來傳回對數值。E、LN2、LN10 是用來傳回自然對數的值。</p>
<pre class="brush: jscript;">
//範例
document.write(Math.log(2));					//結果為 0.6931471805599453
document.write(Math.LOG2E);						//結果為 1.4426950408889634
document.write(Math.LOG10E);					//結果為 0.4342944819032518
document.write(Math.E);							//結果為 2.718281828459045( Euler 常數)
document.write(Math.LN2);						//結果為 0.6931471805599453
document.write(Math.LN10);						//結果為 2.302585092994046
</pre>
<table class="js-tab">
<tr>
<td><span class="jsKeyWord">pow</span> <span class="jsKeyWord">exp</span> <span class="jsType">方法</span></td>
<td class="jsComp">兼容性：IE3+、NN2+、Moz1+、Safari1+</td>
</tr>
</table>
<table class="LN-tab3">
<tr>
<td class="LN-BG1">Math.pow(x, y)</td>
<td class="LN-BG1">x：底數 y：指數</td>
</tr>
<tr>
<td class="LN-BG1">Math.exp(value)</td>
<td class="LN-BG1">value：值或算數運算式</td>
</tr>
</table>
<p>pow 是用來傳回乘冪值。</p>
<p>exp 是用來傳回 e 的乘冪值。</p>
<pre class="brush: jscript;">
//範例
document.write(Math.pow(2,3));					//結果為 8( 2 的 3 次方)
document.write(Math.exp(2));					//結果為 7.38905609893065
</pre>
<table class="js-tab">
<tr>
<td><span class="jsKeyWord">sqrt</span> <span class="jsType">方法</span></td>
<td class="jsComp">兼容性：IE3+、NN2+、Moz1+、Safari1+</td>
</tr>
<tr>
<td><span class="jsKeyWord">SQRT1_2</span> <span class="jsKeyWord">SQRT2</span> <span class="jsType">屬性</span></td>
<td class="jsComp">兼容性：IE3+、NN2+、Moz1+、Safari1+</td>
</tr>
</table>
<table class="LN-tab3">
<tr>
<td class="LN-BG1">Math.sqrt(x)</td>
<td class="LN-BG1">傳回平方根的值</td>
</tr>
<tr>
<td class="LN-BG1">Math.SQRT1_2</td>
<td class="LN-BG1">傳回 1/2 的平方根</td>
</tr>
<tr>
<td class="LN-BG1">Math.SQRT2</td>
<td class="LN-BG1">傳回 2 的平方根</td>
</tr>
</table>
<p>這些是用來傳回平方根的值。</p>
<div style="clear: both;"></div>
<pre class="brush: jscript;">
//範例
document.write(Math.sqrt(4));					//結果為 2
document.write(Math.SQRT1_2);					//結果為 0.7071067811865476
document.write(Math.SQRT2);						//結果為 1.4142135623730951
</pre>
<table class="js-tab">
<tr>
<td><span class="jsKeyWord">max</span> <span class="jsKeyWord">min</span> <span class="jsType">方法</span></td>
<td class="jsComp">兼容性：IE3+、NN2+、Moz1+、Safari1+</td>
</tr>
</table>
<table class="LN-tab3">
<tr>
<td class="LN-BG1">Math.max(Val1,Val2)</td>
<td class="LN-BG1">傳回兩個值當中較大的值</td>
</tr>
<tr>
<td class="LN-BG1">Math.min(Val1,Val2)</td>
<td class="LN-BG1">傳回兩個值當中較小的值</td>
</tr>
<tr>
<td class="LN-BG1" colspan="2">Val1,Val2：值或算數運算式</td>
</tr>
</table>
<p>用來傳回兩個比較後的值。</o></p>
<div style="clear: both;"></div>
<pre class="brush: jscript;">
//範例
document.write(Math.max(1,5));					//結果為 5
document.write(Math.min(7,6));					//結果為 6
</pre>
<table class="js-tab">
<tr>
<td><span class="jsKeyWord">random</span> <span class="jsType">方法</span></td>
<td class="jsComp">兼容性：IE3+、NN2+、Moz1+、Safari1+</td>
</tr>
</table>
<pre class="jsSyntax3">Math.random()</pre>
<p>用來傳回 0 以上未滿 1 的亂數值。</p>
<div style="clear: both;"></div>
<pre class="brush: jscript;">
//範例
document.write(Math.random());					//結果為 0 以上未滿 1 的亂數
document.write(Math.round(Math.random() * 10));	//結果為 0 以上(含0)未滿 11 的整數亂數
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.beyes.tw/333/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
