首先,什么是对象?
JavaScript 中的所有事物都是对象,如:字符串、数值、数组、函数等,每个对象带有属性和方法。
对象的属性:反映该对象某些特定的性质的,如:字符串的长度、图像的长宽等;
对象的方法:能够在对象上执行的动作。例如,表单的“提交”(Submit),时间的“获取”(getYear)等;
Date 日期对象
日期对象可以储存任意一个日期,并且可以精确到毫秒数(1/1000 秒)。
定义一个时间对象 :
<span style="white-space:pre"> </span>var Udate=new Date();
Date对象中处理时间和日期的常用方法:
String 字符串对象
定义字符串的方法就是直接赋值;
var mystr = "I love JavaScript!"
访问字符串对象的属性length:
stringObject.length; 返回该字符串的长度。
stringObject.length; 返回该字符串的长度。
使用 String 对象的 toUpperCase() 方法来将字符串小写字母转换为大写
var mystr="Hello world!";
var mynum=mystr.toUpperCase();
var mynum=mystr.toUpperCase();
返回指定位置的字符
charAt() 方法可返回指定位置的字符。返回的字符是长度为 1 的字符串。
语法:
stringObject.charAt(index); //其中index表示字符在字符串中的下标,第一个字符的下标是0.
<span style="white-space:pre"> </span><script type="text/javascript">
<span style="white-space:pre"> </span> var mystr="I love JavaScript!"
<span style="white-space:pre"> </span>document.write(mystr.charAt(2));
<span style="white-space:pre"> </span></script> 运行结果:l
返回指定的字符串首次出现的位置indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。
语法:
stringObject.indexOf(substring, startpos) //substring是检索的字符串,startpos是检索开始的位置(包括该位置);
<span style="white-space:pre"> </span><script type="text/javascript">
<span style="white-space:pre"> </span>var str="I love JavaScript!"
<span style="white-space:pre"> </span>document.write(str.indexOf("I") + "<br />");
<span style="white-space:pre"> </span>document.write(str.indexOf("v") + "<br />");
<span style="white-space:pre"> </span>document.write(str.indexOf("v",8));
<span style="white-space:pre"> </span></script> 运行结果:0,4,9
字符串分割split() 方法将字符串分割为字符串数组,并返回此数组。
语法:
stringObject.split(separator,limit) //其中separator是分割的字符串数组,limit是返回的数组数量
<span style="white-space:pre"> </span>var mystr = "www.baidu.com";
<span style="white-space:pre"> </span>document.write(mystr.split(".")+"<br>");
<span style="white-space:pre"> </span>document.write(mystr.split(".", 2)+"<br>"); 运行结果:www,baidu,com www,badu
我们可以通过这种方法把字符串变成字符:
<span style="white-space:pre"> </span>document.write(mystr.split("")+"<br>");
<span style="white-space:pre"> </span>document.write(mystr.split("", 5)); 运行结果:w,w,w,.,b,a,i,d,u,.,c,o,m w,w,w,.,b
提取字符串
substring() 方法用于提取字符串中介于两个指定下标之间的字符。
语法:
<span style="white-space:pre"> </span>stringObject.substring(starPos,stopPos)
其中,satrtPos是起始位置,stopPos是结束位置,注意,这两个字符并不会算在内<span style="white-space:pre"> </span><script type="text/javascript">
<span style="white-space:pre"> </span> var mystr="I love JavaScript";
<span style="white-space:pre"> </span>document.write(mystr.substring(7));
<span style="white-space:pre"> </span>document.write(mystr.substring(2,6));
<span style="white-space:pre"> </span></script>
提取指定数目的字符
substr() 方法从字符串中提取从 startPos位置开始的指定数目的字符串。
语法:
<span style="white-space:pre"> </span>stringObject.substr(startPos,length)
这里的startPos也是起始位置,length是获取的字符长度 注意:这里的起始位置包括在内<span style="white-space:pre"> </span><script type="text/javascript">
<span style="white-space:pre"> </span>var mystr="I love JavaScript!";
<span style="white-space:pre"> </span> document.write(mystr.substr(7));
<span style="white-space:pre"> </span> document.write(mystr.substr(2,4));
<span style="white-space:pre"> </span></script> 运行结果是: JavaScript! love
Math对象
然后是方法:
调用形式:
Math.ceil();