js的内部类
JavaScript中本身提供一些,可以直接使用的类,这种类就是内部类。主要有:
Object/Array/Math/Boolean/String/RegExp/Date/Number共8个内部类。
内部类的分类:
从使用方式,把js内部类分为两类(动态类,静态类)。
静态类 使用 类名.属性|方法
比如Math
动态类 使用 var 对象=new 动态类() 对象.属性|方法
//Math
window.alert(Math.abs(-12));
//Date
//显示当前的日期
var nowdate=new Date();
window.alert(nowdate.toLocaleString());//按照一定格式显示。
Math常用的函数
1.abs(x)返回数的绝对值
2.ceil(x)对一个数进行上舍入
3.floor(x)对一个数进行下舍入
4.max(x,y)求x,y中较大的数
5.min(x,y)求x,y中较小的数
6.round(x)对x进行四舍五入
7.random()一个大于0小于1的16位小数位的数字。
window.alert(Math.abs(-12));
//Date
//显示当前的日期
var nowdate=new Date();
window.alert(nowdate.toLocaleString()); alert(Math.ceil(4.5));
alert(Math.floor(4.5));
alert(Math.round(4.7));
alert(Math.round(4.49));
alert(Math.random()); alert(parseInt(Math.random()*100));
alert(Math.round(Math.random()*100));
js内部类--Date类
//Date的常用方法
//window.alert(new Date().toLocaleString()); /*var date=new Date();
window.alert(date);
window.alert(date.getYear()+" "+date.getMonth());*/ function showHello(date){
//小时
var hour=date.getHours();
//分钟
var minute=date.getMinutes(); //1. 思路 把小时和分钟转成 距离00:00的秒数(小时数)
//alert(new Date().toLocaleString());
/*alert(hour);
alert(minute);*/ //把秒数考虑进去
var mysecond=hour*3600+minute*60; if(mysecond>=6*3600&&mysecond<=9*3600){
window.alert("早上好");
}else if(mysecond>=17*3600+31*60&&mysecond<=18*3600+40*60){
window.alert("傍晚好");
} /*var nowTime=date.getHours()+date.getMinutes();
if((nowHour>=6&&nowHour<9)||(nowHour==9&&minute==0)){
window.alert("早上好");
}else if(nowHour>=6&&nowHour<9){ }*/
String类
String是动态类,提供了对字符串的各种操作,下面介绍几个常用的函数。
split();分割字符串。
substr(start,length):从位置start开始截取length个字符
substring(start,end):从位置start开始截取到字符直到end-1结束。
var str="abcd12345";
window.alert(str.length);
var str2="abc def oop";
var arr=str2.split(" ");//如果是split("")则按字符分割。
window.alert(arr); var str3="abcdef";
window.alert(str3.substr(2,3));//cde
window.alert(str3.substring(2,3));//c var str4="abcd";
window.alert("charAt"+str4.charAt(3));
运行结果:
abc def oop
cde
c
d
indexOf的用法
indexOf(str,start)
从位置start开始寻找字符串str,默认情况下start是0(不写的时候),如果找到则返回字符串出现的位置,否则返回-1.
String是一个动态类。
var str5="ab 123 56 123 ab";
window.alert("position:"+str5.indexOf("ab",1));//支持汉字查询
返回14.
Array类
是动态类
看看常用的方法:
pop()删除一个元素并返回最后一个元素
push()添加一个元素
var myarr=new Array();
window.alert(myarr.length);
//动态地添加数据
myarr[0]="sp";
myarr[1]=90;
window.alert(myarr.length+" "+myarr);
//删除数组中的元素
myarr.pop();
window.alert(myarr.length+" "+myarr);
myarr.push("abcd");
window.alert(myarr.length+" "+myarr);
输出结果:0
2 sp,90
1 sp
2 sp,abcd
var arr=new Array(2);
window.alert(arr.length);
arr[0]=1;
arr[1]="a";
arr[2]=2.3;
window.alert(arr.length);
window.alert(arr[3]);
//arr[10]="hello";//不要跳过下标赋值,虽然不会报错并且在第10个位置插入了元素,但是不利于程序
arr['a']="ok";
window.alert(arr.length+" "+arr);
window.alert(arr['a']);
输出:
2
3
undefined
3 2,3,undefined
ok
虽然有arr['a']="ok";这条语句插入了一个元素,但是数组元素个数却没有增加。
js中允许这样访问变量
function Person(){
this.name="xiao";
this.show=function(){
window.alert("hello");
}
}
var p=new Person();
document.writeln(p.name);
document.writeln(p["name"]);
最新文章
- 500lines项目简介
- Linux内核【链表】整理笔记(2)
- 使用while代替for循环的几个习题
- Swift 3 新特性和迁移详解
- MyEclipse for Spring启动时报错";An internal error occurred during: &#39;Updating indexes&#39;.Java heap space";的解决办法
- HDU 3342 Legal or Not (图是否有环)
- 获取股票历史数据和当前数据的API
- C和BlockCode
- 【转】Xcode7.1环境下上架iOS App到AppStore 流程 -- 不错!!
- jquery实现很简单的DIV拖动
- java的抽象类
- VxWorks:添加自己组件到Tornado
- 输入一个正数n,输出所有和为n连续正数序列。例如输入15,由于1+2+3+4+5=4+5+6=7+8=15,所以输出3个连续序列1-5、4-6和7-8。
- [NewLife.XCode]增量累加
- 手动实现Promise
- Oracle中Select语句完整的执行顺序
- SharePoint Framework 配置你的SharePoint客户端web部件开发环境
- C++数组初始化
- H5移动端开发vue+vux
- centos7.4安装过程