今天学习了一下部分 ES6的 数组,函数与对象,做个简单的分享。
数组的高阶方法
- filter 过滤
- forEach 遍历
- 映射 map
- reduce 累计 a,b两个参数 上一次计算结果是这次的a
- every 每个
- some 有一个
数组的新增方法
1. 填充
fill("填充内容",start,end)
区间 [start,end) 取不到end
2. 拷贝与替换 copywithin
copywithin(替换开始位置,拷贝开始位置,拷贝结束位置)
3. 查找 includes findIndex
eg:
var arr = ["山","水","间"];
console.log(arr.includes("间"));
var arr1 =[2,5,423,22,55,100];
var ret = arr1.findIndex(item=>item>300);
console.log(ret)
函数
箭头函数
-
箭头函数的构成
-
箭头函数的this 指向作用域的上一层 this
eg://箭头函数的this指向
var obj = {
age:18,
say:function(){
console.log(“今年我”+this.age)
},
grow:function(){
window.setInterval(()=>{
this.age++;
this.say();
},3000)
}
}
默认参数
function add(a=5,b=10){ }
当参数值为undefined时,使用默认值
不定参数
function add(...args){ }
args 是一个由参数组成的数组
add(1.2) args=[1,2]
add(2,4,6) args = [2,4,6]
拓展参数
var arr = [1,2,3]
function add(a,b,c){ }
add(...arr)
调用函数 把arr数组展开
对象
简写方式
对象的动态属性
动态更改键值