... 扩展运算符能将数组转换为逗号分隔的参数序列
const boy =['刘','黄','李'];// => '刘', '黄', '李',letchunwan=()=>{
console.log(arguments)}chunwan(...boy);//chunwan('刘', '黄', '李')//应用//1.数组合并const a =[1,2,3];const b =[4,5,6];const c =[...a,...b];//c=[1,2,3,4,5,6]//2.数组克隆const a =[1,2]const b =[...a]//b=[1,2]//3.将伪数组转换为真数组<div></div><div></div><div></div>const divs = document.querySelectorAll('div')const divArr =[...divs]//将伪数组divs转换为真数组
//生成器就是一个特殊的函数//yield 函数代码的分隔符 3个分隔符产生4段function*gen(){
console.log(1);yield'一';
console.log(2);yield'二';
console.log(3);yield'三';
console.log(4);}let iterator =gen();
iterator.next();//1
iterator.next();//2
iterator.next();//3
iterator.next();//4for(let v ofgen()){
console.log(v)//一二三}//生成器参数function*gen(arg){
console.log(arg);let one =yield111;
console.log(one);let two =yield222;
console.log(two);let three =yield333;
console.log(three)}//执行获取迭代器对象let iterator =gen('AAA')
console.log(iterator.next());//next方法传入实参
console.log(iterator.next('BBB'))//第一个整体语句返回结构
console.log(iterator.next('CCC'))//第二个整体语句返回结构
console.log(iterator.next('DDD'))//第三个整体语句返回结构//yield语句没有返回值或者说返回值为undefined,但如果next()方法带有参数,则这个参数会被当做上一条yield语句的返回值。//生成器函数实例1functiona(){setTimeout(()=>{
console.log(111);
iterator.next();})}functionb(){setTimeout(()=>{
console.log(222);
iterator.next();})}functionc(){setTimeout(()=>{
console.log(333);
iterator.next();})}functiond(){setTimeout(()=>{
console.log(444);
iterator.next();})}//建立生成器function*fn(){yielda();yieldb();yieldc();yieldd();}let iterator =gen();
iterator.next();//实例2functiongetusers(){setTimeout(()=>{let data ='用户数据';
iterator.next(data)},1000)}functiongetorders(){setTimeout(()=>{let data ='订单数据';
iterator.next(data)},1000)}functiongetgoods(){setTimeout(()=>{let data ='商业数据';
iterator.next(data)},1000)}//建立一个生成器function*gen(){let user =yieldgetusers();
console.log(user);let orders =yieldgetorders();
console.log(orders)let goods =yieldgetgoods();
console.log(goods);}//调用一个生成器let iterator =gen();// 调用gen()函数中的next方法
iterator.next();
promise
const p =newPromise(function(resolve, reject){setTimeout(function(){let data ='数据库中的用户数据'//成功调用resolveresolve(data);let err ='数据库读取失败';reject(err);},1000)});//then方法 value 为正确返回,reason为错误返回
p.then(function(value){
console.log(value);},function(reason){
console.error(reason)})//用promise进行封装const p =newPromise(function(resolve, reject){
fs.readFile('./a.txt',(err,data)=>{if(err)reject(err);resolve(data);})})
p.then(function(value){
console.log(value)},function(reason){
console.log('读取失败')})//catch方法const p =newPromise((resolve, reject)=>{setTimeout(()=>{reject('出错啦')},1000)})//p.then((value) => {},(reason) => {// console.error(reason)//});
p.catch(function(reason){
console.warn(reason);})//then方法
Set
//声明set(对象)
les s =newSet();let s2 =newSet([1,2,3,4,4]);//{1,2,3,4}去重复//元素个数
console.log(s2.size);//4//添加新的元素
s2.add(5);//{1,2,3,4,5}//删除新元素
s2.delete(5)//{1,2,3,4}//检测
console.log(s2.has(1))//true
console.log(s2.has(5))//false//清空
s2.clear()//遍历for(let v of s2){
console.log(v)}//Set 实践let arr =[1,2,3,4,5,4,3,2,1]//1.数组去重let result =[...newSet(arr)];
console.log(result);//2.交集let arr =[1,2,3,4,5,4,3,2,1];let arr2 =[4,5,6,5,6];//has()里面只能带数组,不能带对象集合//new Set()返回的是一个对象集合let result =[...newSet(arr)].filter(item =>{let s2 =newSet(arr2);
console.log(typeof s2);if(s2.has(item)){returntrue;}else{returnfalse}})
console.log(result);//3.并集let union =[...newSet([...arr,...arr2])];
console.log(union);//[1,2,3,4,5,6]//差集let diff =[...newSet(arr)].filter(item =>!(newSet(arr2).has(item)));
console.log(diff);
map
let m =newMap()//添加元素
m.set('name','刘亚亚');
m.set('change',function(){
console.log('很帅')});let key ={
school:'abcd'};
m.set(key,['liu','li','huang'])//删除
m.delete('name');//获取
console.log(m.get('change'))//清空
m.clear()//遍历for(let v of m){
console.log(v)}
(补充)js原型以及原型链
this指向问题补充
// objage中的this指向的是windows的age// f里面的this指向的是objvar name ='liu', age =17;var obj ={
name:'li',
objage:this.age,
f:function(){
console.log(this.name +this.age)}}
console.log(obj.objage);
obj.f();
函数call()
var name ='liu', age =17;var obj ={
name:'li',
objage:this.age,
f:function(){
console.log(this.name +this.age)}}var db ={
name:'li',
age:16}
console.log(obj.f.call(db))//li 15//call用来重定义this这个对象//call传参情况下var name ='liu', age =17;var obj ={
name:'li',
objage:this.age,
f:function(a, b){
console.log(this.name +this.age + a + b)}}var db ={
name:'li',
age:16}
console.log(obj.f.call(db,'王','打枪'))//li 15 王 打枪
const p =newPromise((resolve, reject)=>{resolve('用户数据')})//await 要放在async函数中//await结果是promise正确里面的值asyncfunctionmain(){let result =await p;//
consolet.log(result)}main();//用户数据//失败const p =newPromise((resolve, reject)=>{reject('失败啦')})//await 要放在async函数中//await 失败用try catchasyncfunctionmain(){try{let result =await p;
consolet.log(result)}catch(e){
console.log(e)//失败啦}}main();//用户数据