初学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>
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>
相关文章
- 【说站】javascript中call函数的原理
- 说说JavaScript闭包
- javascript实现的动态添加表单元素input,button等(appendChild)
- JavaScript给汉字排序实例代码
- JavaScript监测ActiveX控件是否已经安装过的代码
- Javascript在IE或Firefox下获取鼠标位置的代码
- JavaScript设计模式富有表现力的Javascript(一)
- javascript常用方法、属性集合及NodeList和HTMLCollection的浏览器差异
- php和javascript之间变量的传递实现代码
- 通过jQuery源码学习javascript(三)
- javascript使用中为什么10..toString()正常而10.toString()出错呢
- javascript通过url向jsp页面传递中文参数导致乱码解决方案