初學(xué)JavaScript第二章
更新時(shí)間:2008年09月30日 00:51:56 作者:
初學(xué)JavaScript時(shí)覺得應(yīng)該注意的一些知識(shí)點(diǎn).(從<<JavaScript高級(jí)編程>>中學(xué)到)
JavaScript的對(duì)象都是實(shí)例化了的,只可以使用而不能夠創(chuàng)建繼承于這些對(duì)象的新的子類.
window對(duì)象為所有對(duì)象的Parent
window對(duì)象的主要屬性有:Name,Length,Parent,Self,Top,Status,Default Status,Opener,Closed.
window對(duì)象的主要方法有:Item,alert,blur,close,confirm,open,focus,showModalDialog.
Document對(duì)象的常用屬性:alinkcolor,Anchors,bgcolor,cookie,domain,embeds,
fgcolor,layers,linkcolor,location,title,url,vlinkcolor
Anchors屬性的使用:
function goNextAnchor(where)
{
window.location.hash = where ;
}
<input type="button" value="下一個(gè)" onClick="goNextAnchor('sec2')"/>
數(shù)組對(duì)象的創(chuàng)建:
function students(name,age)
{
this.name = name ;
this.age = age ;
}
stu1 = new students("thtwin",22) ;
stu = new Array(5) ;
stu[0] = "thtwin" ;
stu[1] = "thtwinj2ee" ;
........
stu.length //數(shù)組的長度
Math對(duì)象的相關(guān)方法使用:
Math.abs(arg) ; //求用戶設(shè)置數(shù)的絕對(duì)值
Math.max(arg1,arg2) ; //返回兩個(gè)數(shù)中的較大值
Math.round(arg1) ; //將浮點(diǎn)數(shù)舍入成它最近的一個(gè)整數(shù)>0.5進(jìn)一,否則丟掉小數(shù)位
Math.floor(arg1) ; //求的是小于或等于變量的值
Math.ceil(arg1) ; //大于或等于變量的值
Math.random() ; //產(chǎn)生一個(gè)0到1之間的隨機(jī)數(shù)
JavaScript中的日期對(duì)象:
該對(duì)象沒有屬性,但是可以通過一些方法來設(shè)置時(shí)間.
禁止使用1970年1月1日之前的時(shí)間.
thisDay = new Date();
thisDay = new Date(month day,year hours:minutes:seconds) ;
thisDay.getYear() ;
thisDay.getMonth() ;
thisDay.getDate() ;//返回一個(gè)月份中的日期值.這個(gè)方法直接返回一個(gè)1以31之間的日期值
thisDay.getDay() ;
thisDay.getTime() ;//返回一個(gè)代表當(dāng)前日期的整數(shù)值.(192687456985)
thisDay.getHours() ;
thisDay.getMinutes() ;
thisDay.getSecondes() ;
thisDay.toLocaleString() ;//返回該時(shí)間的字符串值
With語句的使用
With(Object)
{
statements ;
}
說明:在存取對(duì)象屬性和方法時(shí)不用重復(fù)指定參考對(duì)象.在With語句塊中,凡是JavaScript
不識(shí)別的屬性和方法都和該語句塊指定的對(duì)象有關(guān).如:
當(dāng)使用與Document對(duì)象有關(guān)的write()或者writeln()方法時(shí),往往用如下形式:
document.writeln("Hell!") ;
如果需要顯示大量數(shù)據(jù)時(shí),就會(huì)多次使用同樣的document.writeln() ;語句,這時(shí)就可以
像下面的程序那樣,把所有的以Document對(duì)象為參考的對(duì)象的語句放到With語句塊中,從而
達(dá)到減少語句量的目的.下面是一個(gè)With語句使用的例子:
<script language="javascript">
<!--
With(document)
{
write("thtwin") ;
write("thtwinj2ee") ;
wirte("test") ;
}
//-->
</script>
window對(duì)象為所有對(duì)象的Parent
window對(duì)象的主要屬性有:Name,Length,Parent,Self,Top,Status,Default Status,Opener,Closed.
window對(duì)象的主要方法有:Item,alert,blur,close,confirm,open,focus,showModalDialog.
Document對(duì)象的常用屬性:alinkcolor,Anchors,bgcolor,cookie,domain,embeds,
fgcolor,layers,linkcolor,location,title,url,vlinkcolor
Anchors屬性的使用:
function goNextAnchor(where)
{
window.location.hash = where ;
}
<input type="button" value="下一個(gè)" onClick="goNextAnchor('sec2')"/>
數(shù)組對(duì)象的創(chuàng)建:
function students(name,age)
{
this.name = name ;
this.age = age ;
}
stu1 = new students("thtwin",22) ;
stu = new Array(5) ;
stu[0] = "thtwin" ;
stu[1] = "thtwinj2ee" ;
........
stu.length //數(shù)組的長度
Math對(duì)象的相關(guān)方法使用:
Math.abs(arg) ; //求用戶設(shè)置數(shù)的絕對(duì)值
Math.max(arg1,arg2) ; //返回兩個(gè)數(shù)中的較大值
Math.round(arg1) ; //將浮點(diǎn)數(shù)舍入成它最近的一個(gè)整數(shù)>0.5進(jìn)一,否則丟掉小數(shù)位
Math.floor(arg1) ; //求的是小于或等于變量的值
Math.ceil(arg1) ; //大于或等于變量的值
Math.random() ; //產(chǎn)生一個(gè)0到1之間的隨機(jī)數(shù)
JavaScript中的日期對(duì)象:
該對(duì)象沒有屬性,但是可以通過一些方法來設(shè)置時(shí)間.
禁止使用1970年1月1日之前的時(shí)間.
thisDay = new Date();
thisDay = new Date(month day,year hours:minutes:seconds) ;
thisDay.getYear() ;
thisDay.getMonth() ;
thisDay.getDate() ;//返回一個(gè)月份中的日期值.這個(gè)方法直接返回一個(gè)1以31之間的日期值
thisDay.getDay() ;
thisDay.getTime() ;//返回一個(gè)代表當(dāng)前日期的整數(shù)值.(192687456985)
thisDay.getHours() ;
thisDay.getMinutes() ;
thisDay.getSecondes() ;
thisDay.toLocaleString() ;//返回該時(shí)間的字符串值
With語句的使用
With(Object)
{
statements ;
}
說明:在存取對(duì)象屬性和方法時(shí)不用重復(fù)指定參考對(duì)象.在With語句塊中,凡是JavaScript
不識(shí)別的屬性和方法都和該語句塊指定的對(duì)象有關(guān).如:
當(dāng)使用與Document對(duì)象有關(guān)的write()或者writeln()方法時(shí),往往用如下形式:
document.writeln("Hell!") ;
如果需要顯示大量數(shù)據(jù)時(shí),就會(huì)多次使用同樣的document.writeln() ;語句,這時(shí)就可以
像下面的程序那樣,把所有的以Document對(duì)象為參考的對(duì)象的語句放到With語句塊中,從而
達(dá)到減少語句量的目的.下面是一個(gè)With語句使用的例子:
<script language="javascript">
<!--
With(document)
{
write("thtwin") ;
write("thtwinj2ee") ;
wirte("test") ;
}
//-->
</script>
相關(guān)文章
在JavaScript的正則表達(dá)式中使用exec()方法
這篇文章主要介紹了在JavaScript的正則表達(dá)式中使用exec()方法,是JS入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-06-06JavaScript的strict模式與with關(guān)鍵字介紹
這篇文章主要介紹了JavaScript的strict模式與with關(guān)鍵字,需要的朋友可以參考下2014-02-02深入理解JavaScript系列(41):設(shè)計(jì)模式之模板方法詳解
這篇文章主要介紹了深入理解JavaScript系列(41):設(shè)計(jì)模式之模板方法詳解,模板方法(TemplateMethod)定義了一個(gè)操作中的算法的骨架,而將一些步驟延遲到子類中,模板方法使得子類可以不改變一個(gè)算法的結(jié)構(gòu)即可重定義該算法的某些特定步驟,需要的朋友可以參考下2015-03-03window.navigate 與 window.location.href 的使用區(qū)別介紹
首先說明的是 window.navigate 與 window.location.href 都是實(shí)現(xiàn)頁面鏈接跳轉(zhuǎn)的,下面將介紹它們的區(qū)別。感興趣的朋友可以參考下2013-09-09javascript setinterval 的正確語法如何書寫
setinterval是用來干什么,想必大家都知道了,下面為大家介紹下javascript setinterval 正確的語法,高手勿噴2014-06-06