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

