js基础call() apply(),argument(),date(),math()

这篇博客介绍了JavaScript中的函数方法call()和apply(),它们用于改变函数调用时的上下文。文章还讲解了arguments对象,展示了如何获取函数参数的长度。此外,博主讨论了Date对象的使用,包括获取和设置日期,以及math工具类的各种数学操作。最后,博主探讨了字符串的包装类,列举了一系列字符串操作方法。

call()和apply()
//这两个对象都是函数对象的方法,需要通过函数对象来调用
//apply()方法需要将实参封装到一个数组中统一传递
//arguments()方法
// arguments是实参的对象是一个类数组对象
//arguments.length获取实参的长度

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script type="text/javascript">
		//call()和apply()
		//这两个对象都是函数对象的方法,需要通过函数对象来调用
		//apply()方法需要将实参封装到一个数组中统一传递
			function fun(){
				alert('我是fun函数');
				var obj ={
					name:'obj',
					sayName:function(){
						alert(this.name);
					}
			};
			
			};
			// fun.call(obj,2,3);
			// fun.apply(obj,[2,3]);
			
			//arguments()方法
			// arguments是实参的对象是一个类数组对象
			//arguments.length获取实参的长度
			function fun(){
				console.log(arguments.length);
			}
			fun('hello',true);
			
			
			
			//date对象在js中表示日期
			//输出当前时间
			// document.write(Date())
			//输出指定日期
			var d = new Date(2022,3,26,12,33,0);
			// document.write(d)
			d1 = d.getDate();//获取几号
			console.log(d1);
			d2 = d.getDay();//获取一周中的第几天
			console.log(d2);
			d3 = d.getMonth();//获取月份
			console.log(d3)
			d4 = d.getTime()//获取时间戳
			console.log(d4/1000/60/60/24/365);
			//设定日期
			var myDate = new Date();
			document.write(myDate.setDate(myDate.getDate()+5));//当前日期加5天
			
			
			
			//math工具类,属于一个工具类不需要创建可以直接用
			console.log(Math.round(4.7));
			// 常量有e,pi
			//abs计算一个数的绝对值
			console.log(Math.abs(-99));
			//向上求余
			console.log(Math.ceil(1.1));
			//向下求余
			console.log(Math.floor(1.9));
			//四舍五入
			console.log(Math.round(2.5));
			//最大数
			console.log(Math.max(1,2,344,567));
			//3的2次幂
			console.log(Math.pow(3,2));
			//随机数默认生成0-1之间的数字
			console.log(Math.random()*10);
			console.log(Math.floor(Math.random()*11));
			
			
			//包装类,有三个包装类,通过这3个包装类可以将基本数据类型转换成对象
			//字符串相关方法
			var str = 'hello atguigu';
			console.log(str[1]);//打印字符串的精确位置
			var res = str.charAt(0);
			console.log(res);//返回指定位置的字符
			rest = str.charCodeAt(1);//返回字符串编码
			console.log(rest);
			resu = String.fromCharCode(72);//返回unicode代表的字符串
			console.log(resu);
			resu = str.concat('嘿嘿嘿');//连接一个或者多个字符
			console.log(resu);
			resul = str.indexOf('h');//检索一个字符在一串字符中的位置
			console.log(resul);
			result =str.lastIndexOf('h');//检索一个字符在字符串的位置,反向
			console.log(result);
			result1 = str.split(',');//拆分字符串为数组,参数是分隔符
			console.log(result1);
			result2 = str.toUpperCase();//将字符串转化成大写字母
			console.log(result2);
			result3 = str.toLowerCase();//字符串转换成小写字母
		</script>
	</body> 
</html>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值