zl程序教程

您现在的位置是:首页 >  前端

当前栏目

初学JavaScript第二章

JavaScript 初学 第二章
2023-06-13 09:14:02 时间
JavaScript的对象都是实例化了的,只可以使用而不能够创建继承于这些对象的新的子类.

window对象为所有对象的Parent
window对象的主要属性有:Name,Length,Parent,Self,Top,Status,DefaultStatus,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属性的使用:
functiongoNextAnchor(where)
{
window.location.hash=where;
}
<inputtype="button"value="下一个"onClick="goNextAnchor("sec2")"/>

数组对象的创建:
functionstudents(name,age)
{
this.name=name;
this.age=age;
}
stu1=newstudents("thtwin",22);

stu=newArray(5);
stu[0]="thtwin";
stu[1]="thtwinj2ee";
........
stu.length//数组的长度

Math对象的相关方法使用:
Math.abs(arg);//求用户设置数的绝对值
Math.max(arg1,arg2);//返回两个数中的较大值
Math.round(arg1);//将浮点数舍入成它最近的一个整数>0.5进一,否则丢掉小数位
Math.floor(arg1);//求的是小于或等于变量的值
Math.ceil(arg1);//大于或等于变量的值
Math.random();//产生一个0到1之间的随机数

JavaScript中的日期对象:
该对象没有属性,但是可以通过一些方法来设置时间.
禁止使用1970年1月1日之前的时间.
thisDay=newDate();
thisDay=newDate(monthday,yearhours:minutes:seconds);
thisDay.getYear();
thisDay.getMonth();
thisDay.getDate();//返回一个月份中的日期值.这个方法直接返回一个1以31之间的日期值
thisDay.getDay();
thisDay.getTime();//返回一个代表当前日期的整数值.(192687456985)
thisDay.getHours();
thisDay.getMinutes();
thisDay.getSecondes();
thisDay.toLocaleString();//返回该时间的字符串值

With语句的使用

With(Object)
{
statements;
}
说明:在存取对象属性和方法时不用重复指定参考对象.在With语句块中,凡是JavaScript
不识别的属性和方法都和该语句块指定的对象有关.如:
当使用与Document对象有关的write()或者writeln()方法时,往往用如下形式:
document.writeln("Hell!");
如果需要显示大量数据时,就会多次使用同样的document.writeln();语句,这时就可以
像下面的程序那样,把所有的以Document对象为参考的对象的语句放到With语句块中,从而
达到减少语句量的目的.下面是一个With语句使用的例子:
<scriptlanguage="javascript">
<!--
With(document)
{
write("thtwin");
write("thtwinj2ee");
wirte("test");
}
//-->
</script>