11 js的常用类和方法

String对象

大小写转化(并返回新的字符串):
toUpperCase()
toLowerCase()
字符串截取
substr(start,length)
substring(start,end)
查找字符位置
indexOf()
lastIndexOf()

Date对象

//获取年份:从1900年到现在
alert(today.getYear());
//获取当前年份
alert(today.getFullYear());
//获取月份:从0开始,0表示1月,11表示12月
alert(today.getMonth());
//获取日期:(每月的哪一天)
alert(today.getDate());
//获取星期:周日为0
alert(today.getDay());
//获取时:
alert(today.getHours());
//获取分:
alert(today.getMinutes());
//获取秒:
alert(today.getSeconds());
//获取毫秒
alert(today.getMilliseconds());

Math对象

//向上取整:ceil
// alert(Math.ceil(4.3));
//向下取整:floor
// alert(Math.floor(4.3));
//三角函数:sin cos tan asin acos atan
// alert(Math.sin(Math.PI/6));
//最大最小:max min
// alert(Math.max(2,3,1,5));
//随机数:random :[0,1)之间的伪随机数
// alert(Math.random());
//绝对值:abs
// alert(Math.abs(-9));
//开平方:sqrt
// alert(Math.sqrt(4));
//四舍五入:round
// alert(Math.round(3.6));//4
//log函数:log
// alert(Math.log(10));//底数为E
// alert(Math.log2(8));//底数为2
//指数函数:pow
// alert(Math.pow(2,3));//2的3次方=8

Global对象

Global对象即全局对象,不能直接new,使用的方法就是,直接使用它的方法,无需对象来调用
//eval:将字符串转为js代码并执行
// eval("alert('我是eval转换的代码哟~')");
//parseInt(string,radix):将字符串按照radix为基数解析为一个10进制整数,以下全部解析为15
//如不写radix则默认为十进制
// alert(parseInt("F", 16)); //16进制,F为15
// alert(parseInt("17", 8)); //8进制,17为(8+7=15)8进制转10进制
// alert(parseInt("15", 10)); //15
// alert(parseInt(15.99, 10)); //15 转为整数
// alert(parseInt("FXX123", 16)); //15 因为x不是16进制数,所以后面的不解析
// alert(parseInt("1111", 2)); //15 二进制转十进制
// alert(parseInt("15*3", 10)); //15 *不是10进制数,无法解析,所以后面的也不解析
// alert(parseInt("12", 13)); //15 十三进制转10进制
//isNaN 先Number强转再判断是否为 非Number型,注意,是 非Number型 不是Number型
alert(isNaN('abc'));
alert(isNaN('1ab'));
alert(isNaN('3'));
alert(isNaN(55));
其他方法如escape等还很多,自己去查吧

完整代码

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<!-- 
		 String对象
			大小写转化(并返回新的字符串):
				toUpperCase()
				toLowerCase()
			字符串截取
				substr(start,length) 
				substring(start,end)
			查找字符位置
				indexOf()
				lastIndexOf()
		 Date对象
			//获取年份:从1900年到现在
				alert(today.getYear());
			//获取当前年份
				alert(today.getFullYear());
			//获取月份:从0开始,0表示1月,11表示12月
				alert(today.getMonth());
			//获取日期:(每月的哪一天)
				alert(today.getDate());
			//获取星期:周日为0
				alert(today.getDay());
			//获取时:
				alert(today.getHours());
			//获取分:
				alert(today.getMinutes());
			//获取秒:
				alert(today.getSeconds());
			//获取毫秒
				alert(today.getMilliseconds());
		 Math对象
			//向上取整:ceil
				// alert(Math.ceil(4.3));
			//向下取整:floor
				// alert(Math.floor(4.3));
			//三角函数:sin cos tan asin acos atan
				// alert(Math.sin(Math.PI/6));
			//最大最小:max min
				// alert(Math.max(2,3,1,5));
			//随机数:random :[0,1)之间的伪随机数
				// alert(Math.random());
			//绝对值:abs
				// alert(Math.abs(-9));
			//开平方:sqrt
				// alert(Math.sqrt(4));
			//四舍五入:round
				// alert(Math.round(3.6));//4
			//log函数:log
				// alert(Math.log(10));//底数为E
				// alert(Math.log2(8));//底数为2
			//指数函数:pow
				// alert(Math.pow(2,3));//2的3次方=8
		 Global对象
			Global对象即全局对象,不能直接new,使用的方法就是,直接使用它的方法,无需对象来调用
			//eval:将字符串转为js代码并执行
				// eval("alert('我是eval转换的代码哟~')");
			//parseInt(string,radix):将字符串按照radix为基数解析为一个10进制整数,以下全部解析为15
			//如不写radix则默认为十进制
				// alert(parseInt("F", 16)); //16进制,F为15
				// alert(parseInt("17", 8)); //8进制,17为(8+7=15)8进制转10进制
				// alert(parseInt("15", 10)); //15
				// alert(parseInt(15.99, 10)); //15 转为整数
				// alert(parseInt("FXX123", 16));  //15 因为x不是16进制数,所以后面的不解析
				// alert(parseInt("1111", 2)); //15 二进制转十进制 
				// alert(parseInt("15*3", 10)); //15 *不是10进制数,无法解析,所以后面的也不解析
				// alert(parseInt("12", 13));  //15 十三进制转10进制
			//isNaN 先Number强转再判断是否为 非Number型,注意,是 非Number型 不是Number型
				alert(isNaN('abc'));
				alert(isNaN('1ab'));
				alert(isNaN('3'));
				alert(isNaN(55));
			其他方法如escape等还很多,自己去查吧
		 -->
		 <script type="text/javascript">
			 //String对象测试方法
			 function testString(){
				 //创建字符串
				 var str = "abcdefg";
				 // //转换为大写并返回新的字符串
				 // var str1;
				 // alert(str1 = str.toUpperCase());
				 // alert(str);//源字符串不变
				 // //转换为小写并返回新的字符串
				 // alert(str1.toLowerCase());
				 // //字符串截取并返回新的字符串
					// //substr
				 // alert(str.substr(1,3))
				 // alert(str);//原字符串不变
					// //substring 含头不含尾
				 // alert(str.substring(0,2));
				 //查找字符位置
				 alert(str.indexOf("c"));
				 alert(str.lastIndexOf("a"));
			 }
			 //测试Date对象
			 function testDate(){
				 var today = new Date();
				 //获取年份:从1900年到现在
				 alert(today.getYear());
				 //获取当前年份
				 alert(today.getFullYear());
				 //获取月份:从0开始,0表示1月,11表示12月
				 alert(today.getMonth());
				 //获取日期:(每月的哪一天)
				 alert(today.getDate());
				 //获取星期:周日为0
				 alert(today.getDay());
				 //获取时:
				 alert(today.getHours());
				 //获取分:
				 alert(today.getMinutes());
				 //获取秒:
				 alert(today.getSeconds());
				 //获取毫秒
				 alert(today.getMilliseconds());
			 }
			 function testMath(){
				 //向上取整:ceil
				 // alert(Math.ceil(4.3));
				 //向下取整:floor
				 // alert(Math.floor(4.3));
				 //三角函数:sin cos tan asin acos atan
				 // alert(Math.sin(Math.PI/6));
				 //最大最小:max min
				 // alert(Math.max(2,3,1,5));
				 //随机数:random :[0,1)之间的伪随机数
				 // alert(Math.random());
				 //绝对值:abs
				 // alert(Math.abs(-9));
				 //开平方:sqrt
				 // alert(Math.sqrt(4));
				 //四舍五入:round
				 // alert(Math.round(3.6));//4
				 //log函数:log
				 // alert(Math.log(10));//底数为E
				 // alert(Math.log2(8));//底数为2
				 //指数函数:pow
				 // alert(Math.pow(2,3));//2的3次方=8
			 }
			 function testGlobal(){
				 //eval:将字符串转为js代码并执行
				 // eval("alert('我是eval转换的代码哟~')");
				 //parseInt(string,radix):将字符串按照radix为基数解析为一个10进制整数,以下全部解析为15
				 //如不写radix则默认为十进制
				 // alert(parseInt("F", 16)); //16进制,F为15
				 // alert(parseInt("17", 8)); //8进制,17为(8+7=15)8进制转10进制
				 // alert(parseInt("15", 10)); //15
				 // alert(parseInt(15.99, 10)); //15 转为整数
				 // alert(parseInt("FXX123", 16));  //15 因为x不是16进制数,所以后面的不解析
				 // alert(parseInt("1111", 2)); //15 二进制转十进制 
				 // alert(parseInt("15*3", 10)); //15 *不是10进制数,无法解析,所以后面的也不解析
				 // alert(parseInt("12", 13));  //15 十三进制转10进制
				 //isNaN 先Number强转再判断是否为 非Number型,注意,是 非Number型 不是Number型
				 alert(isNaN('abc'));
				 alert(isNaN('1ab'));
				 alert(isNaN('3'));
				 alert(isNaN(55));
			 }
		 </script>
		 
		 <h1>js的常用类和方法</h1>
		 <hr>
		 <button type="button" onclick="testString()">测试String对象</button>
		 <button type="button" onclick="testDate()">测试Date对象</button>
		 <button type="button" onclick="testMath()">测试Math对象</button>
		 <button type="button" onclick="testGlobal()">测试Global对象</button>
	</body>
</html>

  

posted @ 2020-01-18 16:06  Scorpicat  阅读(203)  评论(0编辑  收藏  举报