JavaScript基础回顾知识点记录3
-
js 中 垃圾回收
//将不在使用的对象设置为null , js就会自动进行垃圾回收机制 var obj = {}; obj = null;
-
js 中 数组基本介绍
- 数组也是一个对象
- 与普通对象功能类似,用来存储值。对象用字符串作为属性名(索引),数组用数字(从0开始)作为索引
- 数组的存储性能比普通对象好
//数组的基本操作 var arr = new Array(); var arrs = []; // 推荐使用这种方式创建数组 arr[0] = 1; arr[1] = 2; arrs[0] = 11; arrs[11] = 22; console.log(arr); //输出 [1, 2] console.log(arrs); //输出[11, empty × 10, 22] console.log(arr.length); //输出2 console.log(arrs.length); //输出12 arr.length = 1; console.log(arr); //输出[1] 注意: 1、对于连续数组,使用length可以获取数组的长度; 对于非连续数组,使用length会获取到数组的最大索引+1 2、修改数组长度,如果修改值大于原长度,则默认添加元素个数。如果小于原长度,则删减元素个数
-
js 中 数组常用方法介绍
var arr = ["zhangsan","lisi","wangwu"]; // push() 方法,向数组末尾添加一个或多个元素,并返回添加后的数组长度 var res = arr.push("qianliu","zhaoqi"); // 查看push的返回值 console.log(arr); //输出["zhangsan", "lisi", "wangwu", "qianliu", "zhaoqi"] console.log(res); //输出5,返回值为数组长度 // pop() 方法,删除数组最后一个元素,并返回被删除元素 var res2 = arr.pop(); console.log(res2); //输出["zhangsan", "lisi", "wangwu", "qianliu"] console.log(arr); //输出zhaoqi,返回值为被删除元素 // slice() 方法 从数组提取指定元素。该方法不会改变原数组,封装到一个新数组中返回。 // 第一个参数:截取开始位置的索引,包含开始索引。第二个参数:截取结束位置的索引,不包含结束索引。 // 第二个参数可以不写,默认从开始位置全部截取。 // -1 表示倒数第一个,-2表示倒数第二个 var res3 = arr.slice(0,2); var res4 = arr.slice(0); var res5 = arr.slice(-1); var res6 = arr.slice(-2); var res7 = arr.slice(0,-1); console.log(res3); //输出["zhangsan", "lisi"] console.log(res4); //输出["zhangsan", "lisi", "wangwu"] console.log(res5); //输出["wangwu"] console.log(res6); //输出["lisi", "wangwu"] console.log(res7); //输出["zhangsan", "lisi"] // splice() 方法, 删除数组中指定的元素,操作的是元素组,并返回被删除元素 // 第一个参数表示开始位置的索引,第二个参数表示删除的数量 var res8 = arr.splice(1,2); console.log(arr); //输出["zhangsan"] console.log(res8) //输出["lisi", "wangwu"] // 其他函数不多加赘述,需用的时候查手册即可
-
js 中 数组遍历
//for循环遍历 for(var i = 0; i<arr.length;i++) { console.log(arr[i]); } //forEach遍历; (这个方法仅支持IE8以上的浏览器使用) //该方法需要回调函数作为参数 ,该回调函数中传递三个参数: //第一个参数:当前遍历的元素值。第二个参数:当前遍历的索引值。第三个参数:当前遍历的数组 arr.forEach(function(value,index,obj){ console.log(value); console.log(index); console.log(obj); })
-
js 中 apply方法和call方法
// 这两个方法都是函数对象的方法。 // 调用这两个方法后,可以将一个对象指定为第一个参数,此时该函数执行时的this将变为这个对象。 // call()方法可以将实参在对象之后依次传递 // apply()方法需要将实参封装成一个数组再传递 function fun(a,b) { console.log(a); console.log(b); console.log(this); } var obj = {name:"zhangsan"}; fun(1,2); //this输出window fun.call(obj,1,2); //this输出{name:"zhangsan"} fun.apply(obj,[1,2]); //this输出{name:"zhangsan"}
-
js 中 arguments(实参列表)
// 在调用函数时,浏览器会传递两个隐含参数 // 第一个:函数的上下文对象 this; 第二个:封装实参的对象arguments(类数组对象) // 传递的所有实参都会被封装到arguments中 // 不定义形参时,仍可以使用arguments来使用实参 function fun(){ console.log(arguments[0]); //输出Arguments(3)["hello", true, "world", callee: ƒ, Symbol(Symbol.iterator): ƒ] console.log(arguments.length); //输出3,实参数量 } fun('hello',true,'world');
-
js 中 Date对象 (时间)
// 创建Date对象 var d = new Date(); //当前时间 var d2 = new Date("12/03/2022 12:12:12"); //创建指定时间(日期格式为:月份/日/年/ 时:分:秒) var date = d2.getDate(); // 获取当前日期是几日 var day = d2.getDay(); // 获取当前日期周几
-
js 中 Math
//绝对值 console.log(Math.abs(-1)); //输出1 //向上取整 console.log(Math.ceil(2.1)); //输出3 //向下取整 console.log(Math.floor(2.1)); //输出2 //四舍五入取整 console.log(Math.round(2.1)); //输出2 //生成随机数 x-y之间的随机数: Math.round(Math.random()*(y-x)+x) console.log(Math.round(Math.random()*19)+1); //1-20的随机数 //获取多个数中的最大值/最小值 var max = Math.max(10,11,12,13); var min = Math.min(1,2,3,4); console.log(max); //输出13 console.log(min); //输出1
标签:
JavaScript
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构