9.函数
ES6 允许为函数的参数设置默认值,即直接写在参数定义的后面。通常情况下,定义了默认值的参数,应该是函数的尾参数
函数的length属性,将返回没有指定默认值的参数个数
如果设置的默认值不是函数的尾参数,那么length只计算默认值前面的参数的长度
参数默认值可以与解构赋值的默认值,结合起来使用。
1)形参默认值
let myFun1 = function(a,b,c = 10){
console.log(a,b,c);
console.log(arguments); //ES5中,实参列表,类数组对象
}
myFun1(null,2);
//null 2 10
// [Arguments] { '0': null, '1': 2 }
let myFun = function(x,y=123,z){
console.log(x,y,z);
}
myFun();
console.log(myFun.length); //1
2)解构
//2. 对象解构
let myFun2 = function({a,b,c}){
console.log(a,b,c);
}
myFun2({c:'wll',a:1,b:2}); //1 2 wll
//3. 数组解构
let myFun3 = function([a,b,c]){
console.log(a,b,c);
}
myFun3([1,2,3]); //1 2 3
3)rest参数
//4. ES6 引入 rest 参数(形式为...变量名),用于获取函数的多余参数,这样就不需要使用arguments对象了。
// ...用在函数的形参位置,叫做rest参数,也叫拓展运算符的逆运算
let myFun4 = function (a,...b){
console.log(a,b);
}
myFun4(1,2,3,4,5,6) //1 [ 2, 3, 4, 5, 6 ]
4)箭头函数
this指向
普通函数:this指向调用者,没有调用者指向global
箭头函数:没有自己的this,this指向'声明时'外部作用域的this
// 箭头函数,指向声明时外部作用域的this
let arrowFun = ()=>{
console.log(this); //{}全局
}
let obj = {
name:'wll',
age:18,
gender:'female',
sayName:()=>{
console.log(this); //指向全局
},
sayAge(){
return ()=>{
console.log(this); //指向obj
}
},
sayGender(){
return arrowFun; //指向全局
}
}
obj.sayName();
obj.sayAge()();
obj.sayGender()();
arrowFun();
this.name = 'cjk';
module.exports.gender = 'male';
// 默认情况下,全局this和module.exports一样
console.log('全局this',this); //全局this { name: 'cjk', gender: 'male' }
console.log('module默认导出',module.exports); //module默认导出 { name: 'cjk', gender: 'male' }
console.log(this === module.exports); //true
箭头函数写法
// 箭头函数的极简形式 只有一个形参,有且只有一条返回语句
//形参只有一个时,可省略括号,只有一句返回语句时,箭头后面可直接写表达式
let result = arr.filter(item => item > 5)
// ES6箭头函数完整写法
let result = arr.filter((item) => {
retrun item > 5
})
// ES5写法
let result = arr.filter(function(item) {
retrun item > 5
})
10.迭代器Iterator
实现了iterator接口的数据,即是可迭代的数据
1)作用
1)为实现iterator接口的数据,提供统一的访问机制(for of循环) 2)数据内部成员按照一定的次序排列,通过调用.next()方法,使指针向下移动,指向下一个成员 3)供ES6中的for of 循环来调用
2)访问
iterator.next() 通过调用.next方法,可以使迭代器对象中的指针向下移一位 value有值,done就为false value为undefined时,done为true
let arr1 = ['tom','vicky','gicky'];
let values = arr1.values();
//console.log(typeof(values.next())); //object
console.log(values.next()); //values.next()打印出来是个对象
//{ value: 'tom', done: false }
console.log(values.next());
//{ value: 'vicky', done: false }
console.log(values.next());
//{ value: 'gicky', done: false }
console.log(values.next());
{ value: undefined, done: true }
3)for of 使用
可以遍历实现了iterator接口的数据
// for of
let arr1 = ['tom','vicky','gicky'];
let values = arr1.values();
for(let value of values){
console.log(value);
}
//tom vicky gicky
4)for of 实现(.next())
let arr1 = ['tom','vicky','gicky'];
let values = arr1.values();
let item ;
while(!(item = values.next()).done){
console.log(item.value);
}
//tom vicky gicky
5)原生具备 Iterator 接口的数据
Array、Map、Set、String、arguments、NodeList 等
Symbol.iterator(iterator接口)指向数据的默认迭代器生成方法
let iterator1 = [1,2,3][Symbol.iterator]()
console.log(iterator1); //Object [Array Iterator] {}
let iterator = 'hello'[Symbol.iterator]()
console.log(iterator); //Object [String Iterator] {}
console.log(iterator.next()); //{ value: 'h', done: false }
console.log(iterator.next()); //{ value: 'e', done: false }
console.log(iterator.next()); //{ value: 'l', done: false }
console.log(iterator.next()); //{ value: 'l', done: false }
console.log(iterator.next()); //{ value: 'o', done: false }
console.log(iterator.next()); //{ value: undefined, done: true }
// [Symbol.iterator] 接口存在在原型对象上
String.prototype[Symbol.iterator]==='hello'[Symbol.iterator]() // true
11.Set集合
类似于数组,key和value是相同
1)特性
不允许重复值出现
应用:数组去重
Array.form(new Set(arr))
2)API
Set.prototype.size 返回Set实例的成员总数。
Set.prototype.add(value) 添加某个值,返回Set结构本身
Set.prototype.delete(value) 删除某个值,返回一个布尔值,表示删除是否成功。
Set.prototype.has(value) 返回一个布尔值,表示该值是否为Set的成员。
Set.prototype.clear() 清除所有成员,没有返回值。
Set.prototype.keys() 返回键名的遍历器
Set.prototype.values() 返回键值的遍历器
Set.prototype.entries() 返回键值对的遍历器
Set.prototype.forEach() 使用回调函数遍历每个成