es6更新

来源:互联网 发布:mac的usb配件已停用 编辑:程序博客网 时间:2024/06/05 11:18

一、  let  

//外面不能访问到a ,也不能重新声明,不能重新赋值 

let a=1; 
let a=2; 错误,不能重新声明
let a=1;
    a=2; 错误,不能重新赋值

二、const  //必须有初始值

const a      //错误 要有初始值,不能重新赋值      a=1; 
const b=1;
const b=2;  //错误 不能重新赋值

三、``  使用 ``     //字符串链接

    

let a=`1111`;let b=`  22222${a}22222  `

四、解构赋值

var [a,b,c]=[12,14,16];console.log(a,b,c)

五、复制数组

let arr=[1,2,3]arr1=Array.from(arr);arr1.pop();console.log(arr)   //arr1=[1,2] //直接复制数组,是直接赋值内存指针,用Array.from(),是直接赋值
let arr5=[...arr];  //第二种直接用...arr5.pop();console.log(arr);
接受参数
function show(...args){  console.log(args);  //...集中接收参数到args}show(1,2,3,4);

六、 循环

function fide(){     let arr=['apple','orlenf','red']             for(let i of arr){  //for of 表示取的数组的value                console.log(i);            }        } fide();

 let map=new Map();     //map.set() 添加    //map.get() 获取    //map.delete() 删除    map.set('a','apple');    map.set('b','apple1');    map.set('v','apple2');    map.set('d','apple3'); for(var [key,value] of map){      console.log(key,value);  }

七、箭头函数

参考https://segmentfault.com/a/1190000004280772

https://pjchender.blogspot.com/2017/01/es6-arrow-function.html





let fn = function(){              console.log(this.constructor.name);  // Object(data)              setTimeout(function(){                console.log(this.constructor.name) // Window              },100);            }                        // 箭頭函式 Arrow function            let fn_arr = function(){              console.log(this.constructor.name);  // Object(data)              setTimeout(() => {                console.log(this.constructor.name) // Object(data)              },100);            }            let id = 21;            let data = {              id: 21            }            fn.call(data);                 fn_arr.call(data); 

总结:箭头函数没有像function (){  } 中的大括号这样的执行作用域,只要使用箭头函数,他的作用域就是使用箭头函数的这个作用域。

所以在setTimeout中使用箭头函数,箭头函数里面的this就是指向当前箭头函数所处的作用域中,就是data.fn_arr这个作用域中,里面的this就是指向对象data

https://www.w3cplus.com/javascript/es6-arrow-function.html






原创粉丝点击