js常用的方法和对象

常用方法:

		常用的对象和方法:
			1、String
			2、Date
			3、Math
			4、Global
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>js常用方法和对象</title>
		<script type="text/javascript">
		
			function testString(){
				//1String对象学习
			var str=new String ("abcdefg");
			var str2="MNP";//简写形式
			//字符串大小写的转换
			alert(str.toUpperCase());//字符串小写转换成大写//ABCDEFG
			alert (str2.toLowerCase());//大写转换成小写//mnp
			
			//字符串的切割
			var s="哈哈,呵呵,嘿嘿";
			var s1=s.split(",");//按照指定的字符切割字符串,返回数组
			alert(s1.length);//1
			alert(s1);//[哈哈,呵呵,嘿嘿]
			alert(typeof s);//string
			alert(typeof s1);//object
			
			//字符串的截取
			var s2="abcdef";
			alert(s2.substr(1,3));//从指定的开始位置截取指定长度的子字符串//bcd
			alert(s2.substring(1,3));//从指定的开始位置和指定的结束位置截取子字符串,含头不含尾。//bc
			
			var s3="abcdefg";
			alert(s.indexOf("dd"));//返回指定子字符串第一次出现的角标,没有则返回-1;//-1
			alert(s3.indexOf("f"));//5
			}
			
			function testDate(){
				//1、创建Date对象
				var d=new Date();
				alert(d);//Fri Dec 04 2020 12:53:35 GMT+0800 (中国标准时间)
				//获取当前年月日时分秒
				alert(d.getYear());//120//返回的是1900年开始距今的年分数
				alert(d.getFullYear());//2020//返回的是当前的年份
				alert(d.getMonth()+1);//12//返回的当前月份的角标值,需要+1
				alert(d.getDate());//4//返回的是当前的日期数
				alert(d.getDay());//5//返回的是当前的星期数,但是周天会返回值为0;
				alert(d.getHours());//12//返回当前时间的小时数
				alert(d.getMinutes());//53//返回当前时间的分钟数
				alert(d.getSeconds());//35//返回当前时间的秒数
				alert (d.getHours()+":"+d.getMinutes()+":"+d.getSeconds());//12:53:35
			}
			
			function testMath(){
				//1.Math在使用的时候不能new,使用Math.方法名调用即可
				//创建随机数字
				alert("Math.random();"+Math.random());//Math.random();0.25370267227756504//返回0-1之间的随机数字,含0不含1
				alert(1000+Math.random()*9000);//6277.468764801932
				//向下取整
				alert (Math.floor("12.34"));//12
				alert(Math.floor(1000+Math.random()*9000));//3105
				//向上去整
				alert(Math.ceil("12.34"));//13
				//四舍五入
				alert(Math.round(12.12));//12
				alert(Math.round(12.65));//13
				
			//数值比较:求取最小值,最大值
			alert(Math.min(12,13,5,78));//5//获取最小值
			alert(Math.max(12,3,4,56));//56//获取最大值
	
			}
			function TestGlobal(){
				//4、Global对象学习
					//1、改对象从不直接使用并且不能new,也是就直接写方法名调用即可。
						//使用eval将字符串转换为可执行的js代码
				var str="var a=123";
				eval(str);
				alert(a);//123
				//使用isNaN判断是否值NaN
				alert(isNaN("123"));//false
				//获取字符中的浮点数
				alert(parseFloat("12.34a34a"));//12.34
				alert(parseFloat("12a34.5a"));//12
				alert(parseInt("a12a5s3"));//NaN
				alert(parseInt("12.34a5s3"));//12
			}
		</script>
	</head>
	<body>
	<input type="button" value="String学习" onclick="testString()" />
	<input type="button" value="Date学习" onclick="testDate()" />
	<input type="button" value="Math学习" onclick="testMath()" />
	<input type="button" value="Global学习" onclick="TestGlobal()" />
	</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值