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>

<think>我们被要求逐行解析给定的Kotlin代码片段,该代码片段是一个when表达式,处理Flutter方法调用中的"getContacts"方法。 代码片段: when (call.method) { "getContacts" -> asyncTask(result) { this.getContacts( query = call.argument("query"), withThumbnails = call.argument<Any?>("withThumbnails") == true, photoHighResolution = call.argument<Any?>("photoHighResolution") == true, limit = call.argument("limit"), offset = call.argument("offset"), sortBy = call.argument("sortBy"), phoneQuery = call.argument("phoneQuery")) } 我们将逐行解析其功能和结构。 </think>以下是针对提供的 Kotlin 代码的逐行解析: ### 代码逐行解析 ```kotlin when (call.method) { // 1. 方法调用分发:根据Flutter调用的方法名进行路由 "getContacts" -> // 2. 匹配特定方法:当方法名为"getContacts"时执行 // 3. 异步任务封装:创建异步任务并传递MethodChannel.Result回调 asyncTask(result) { // 4. 调用实际业务方法:执行获取联系人数据的核心逻辑 this.getContacts( // 5. 参数解析:从Flutter调用中提取查询参数 query = call.argument("query"), // 字符串类型查询条件 // 6. 布尔参数转换:处理可能为null的布尔值参数 withThumbnails = call.argument<Any?>("withThumbnails") == true, // 是否包含缩略图 // 7. 高分辨率处理:确定是否获取高清照片 photoHighResolution = call.argument<Any?>("photoHighResolution") == true, // 高清图片标志 // 8. 分页参数:限制返回结果数量 limit = call.argument("limit"), // 整数类型数量限制 // 9. 分页偏移:结果集起始位置 offset = call.argument("offset"), // 整数类型偏移量 // 10. 排序参数:指定排序字段 sortBy = call.argument("sortBy"), // 字符串类型排序字段 // 11. 电话查询:电话号码过滤条件 phoneQuery = call.argument("phoneQuery") // 字符串类型电话查询 ) } // 12. 异步任务结束 } // 13. when表达式结束 ``` ### 关键技术要点解析 1. **方法路由机制** - `call.method` 获取Flutter端调用的方法名称 - `when` 表达式实现方法路由分发 - 支持扩展其他方法处理分支(如"addContact"、"updateContact") 2. **参数安全解析** - `call.argument<T>()` 泛型方法提取参数 - 空安全处理:使用 `== true` 将可空值转为非空布尔值 - 类型推断:Kotlin自动推断参数类型(如`limit`/`offset`为`Int?`) 3. **异步任务设计** - `asyncTask()` 封装异步操作(假设是自定义扩展函数) - 隐式捕获`result`:用于向Flutter返回结果或错误 - 避免主线程阻塞:防止联系人查询导致UI卡顿 4. **联系人查询参数** | 参数名 | 类型 | 说明 | 默认处理 | |--------|------|------|----------| | `query` | String? | 姓名/备注模糊查询 | `null`表示无限制 | | `withThumbnails` | Boolean | 是否加载缩略图 | `false` | | `photoHighResolution` | Boolean | 是否高清照片 | `false` | | `limit` | Int? | 最大返回数量 | `null`表示无限制 | | `offset` | Int? | 结果偏移量 | `0` | | `sortBy` | String? | 排序字段 | `DISPLAY_NAME` | | `phoneQuery` | String? | 电话号码精确查询 | `null` | 5. **实际执行流程** ```mermaid graph TD A[Flutter调用getContacts] --> B{方法路由} B -->|匹配成功| C[启动asyncTask] C --> D[解析所有参数] D --> E[执行getContacts查询] E --> F[处理结果/异常] F --> G[通过result回调返回] ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值