您的位置:首页 > Web前端 > JavaScript

js_day18--js内部类和系统函数

2013-12-23 12:55 344 查看
Day18

●js内部类

▶Javascript中本身提供一些可以直接使用的类(有属性和方法了)。主要有:

Object、Array、Math、Boolean、String、Date、Number、RegExp(正则表达式)

 

▶分类:

动态类(必须创建实例才能调用方法),比如Date:

//Date
//显示当前时间
var nowdate = new Date();
window.alert(nowdate.toLocaleString());


静态类(可以直接通过类名调用方法),比如Math:

//Math

                    window.alert(Math.abs(-12))

★js内部类—Math

      Math是静态类,提供了常用的数学函数和方法
      看看几个常用的方法:

alert(Math.ceil(4.5));//返回5

alert(Math.floor(4.5));//返回4

alert(Math.round(4.77));//返回5

alert(Math.random());//返回一个随机的16位小数

//如何得到一个1--100的随机数字

alert(Math.round(Math.random()*100));


★js内部类—Date

      Date() 返回当前日期和时间

      getDate()
从Date对象返回一个月中的某一天

      getDay() 从Date对象返回一周中的某一天

      getMonth()
从Date对象中返回月份

      getYear() 从Date对象中返回年

      getHours() 
从Date对象中返回小时数

      getMinutes()
从Date对象中返回分钟

      getSeconds() 
从Date对象中返回秒数

题目:



js代码:

     var date = new Date();
function showHello(date){
//得到当前的小时数
var hour = date.getHours();
//得到当前分钟
var minutes = date.getMinutes();
var time;
//拼接时间
if(minutes<10){
time = date.getHours()+"0"+date.getMinutes();
window.alert(time);
}else{
time = date.getHours()+""+date.getMinutes();
window.alert(time);
}
time = parseInt(time);
if(time>=600&&time<=900){
window.alert("早上好!");
} else if(time>=901&&time<=1130){
window.alert("上午好!");
}else if(time>=1131&&time<=1430){
window.alert("中午好!");
}else if(time>=1431&&time<=1730){
window.alert("下午好!");
}else if(time>=1731&&time<=1840){
window.alert("傍晚好!");
}else if(time>=1841&&time<=2400){
window.alert("晚上好!");
}else if(time>=0&&time<=559){
window.alert("凌晨好!");
}
}
showHello(date);


★js内部类—String

      String
是动态类,提供了对字符串的各种操作,常用的几个如下:

      indexOf() 返回某个字符串值在该字符串中首次出现的位置

      split() 把字符串分割为字符串数组

      substr() 提取从start下标开始的指定数目的字符

      substring() 
提取字符串中介于两个指定下标之间的字串

      charAt() 返回指定位置的字符

      length
属性,可以得到字符串的长度

      toString() 
js中所有内部对象的成员方法,作用是将对象中的数据转成某个格式的字符串

      match()\replace()\search()用的很多,但是涉及到正则表达式,这三个函数放在正则表达式章节介绍
        常用案例举例:

var str = "abcd12345";
window.alert(str.length);//9

var str2 = "abc|def|oop";
var arr = str2.split("|");//分割,如果是("")就是一个一个分割了
window.alert(arr);

var str3 = "abcdef";
window.alert(str3.substr(1,3));//bcd,从1开始,取3个字符
window.alert(str3.substring(1,3));//bc 取1、2位

var str4 = "abcd";
window.alert("charAt "+str4.charAt(3));//d,下标为3的单个字符

var str5 = "ab 123 59 ab";
window.alert("indexof "+str5.indexOf("ab"));//打出0,返回第一次出现ab的位置
window.alert("indexof "+str5.indexOf("ab",1));//打出10,返回从1之后的,第一次出现ab的位置
window.alert("uyyy");//-1,因为找不到


★js内部类—Array

      Array类是一个动态类。

      Array类提供了对数组的操作,使用Array类可以轻松的创建数组,并对数组进行删除,排序和合并等操作。

      常用案例举例:
var myarr = new Array();
//动态地添加数据
myarr[0] = "Gavin";
myarr[1] = "20";
window.alert(myarr.length + " "+myarr);
window.alert(myarr.pop());//删除最后一个元素,相当于出栈
window.alert(myarr.length + " "+myarr);
myarr.push("abcd");//相当于入栈,在数组末尾添加一个元素
window.alert(myarr.length + " "+myarr);

var myarr2 = new Array(2);
window.alert("length:"+myarr2.length);
myarr2[0]=3;
myarr2[1]=32;
myarr2[2]=12;
myarr2[9]="Gavin";//不要跳过下标放值
window.alert("length:"+myarr2.length+" "+myarr2);//length为10
myarr2['a'] = "Ok";//也可以用,但不推荐使用


★js内部类—Boolean

                  两个方法: toString();

valueOf();

★js内部类—Number

        Number类是该对象原始数值的包装类。
                
常用案例举例:

var a = 890;
b = a.toString();//b是字符串

var c = 23.45367;
c = c.toFixed(2);//保留两位小数
window.alert(c);//23.45
//输出23的二进制和十六进制
var d = 23;
window.alert(d.toString(2));//10111
window.alert(d.toString(16));//17
window.alert(d.toString(8));//27
window.alert(d.toString(9));//输出25,虽然9进制不常用,但可以转换
window.alert(Number.MAX_VALUE);//输出当前浏览器支持的最大数


●js系统函数





var a = 90;
window.alert(isNaN(a));//判断它是不是 不是一个数?是一个数返回false
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: