注:ES6的一些新属性会显示语法错误,不过不会影响效果,在Languages里面也可以调;
Let: used to define variables
特点:只能在代码块里面使用,let拥有块级作用域;并且let不允许重复声明;比如: var a = 12; var a = 5; alert(a);//5; let a = 12; let a = 5; alert(a);//报错;Identifier ‘a‘ has already been declared代码块:{}包起来的代码,形成了一个作用域,比如if,for,while,json除外;块级作用域;var 只有函数作用域;用处: 自执行函数: for(let i = 0; i < aBtn.length; i++){ aBtn[i].onclick = function(){ alert(i);//0,1,2 } }*** 块级作用域其实就是匿名函数自执行;
Const: Typically used to define constants
常量:不允许修改;有些时候定的变量不希望被改变,这个不希望被改变的值就是常量;比如:let a = 5; a = 12; alert(a);//12;其实希望a的值是5,但是下面有相同的变量名字覆盖了let a的值;这个时候就要用到const;
Once the const is assigned, it can no longer be modified in the future, such as:
const a = 12;a = 5;alert(a);//Assignment to constant variable.
The const must give the initial value, such as:
const a;a = 12;alert(a);//Missing initializer in const declaration;正确的写法: const a = 12;因为const定义的变量以后再也没法赋值了;
Const cannot be declared repeatedly, for example:
const a = 12;const a = 5;alert(a);//Identifier ‘a‘ has already been declared;用途:防止意外修改变量;
About String joins
之前定义字符串: var str = ‘‘;或者 var str = "";ES6定义字符串: var str1 = `elcome; var str2 = `w`;ES6连接字符串:${变量名} var str3 = `${str2}${str1}`; //输出:welcome;
Deconstruction Assignment
ES6之前: //var a = 12; //var b = 5; //var c = 10; //alert(a);ES6: var [a,b,c] = [12,5,10];//数组的形式定义; alert(a);//访问某一项 console.log(a);//打印出全部的值; var {a,b,c} = {a:12,b:5,c:10};//json的形式定义;跟顺序无关; alert(a);//访问某一项; console.log(a,b,c);//打印出全部的值;使用ES6定义的时候注意模式匹配: 模式匹配:左边的样子和右边的一样; var [a,[b,c],d] = [1,[3,4],5]; console.log(a,b,c,d);解构赋值默认值的写法: var {time=12,id=321} = {}; console.log(time,id);//这里的time和id都是默认值;因为等号右边没有赋值;所以需要给默认值,如果不给默认值的的话是undefined;
Array.from (): Copy an array
var arr = [1,2,3,4];var arr2 = Array.from(arr);arr2.pop();//删除arr2中的某一项不会影响到arr;console.log(arr,arr2);
Quickly copy an array: ...
实例1: var arr = [1,2,3,4]; var arr2 = [...arr1];//引用arr1里面的内容; arr2.pop(); console.log(arr2);实例2: function show(...args){ console.log(args);//1,2,3,4;这里的值也就是arguments的每一个; args.push(5);//往arguments里面添加一个; console.log(args);//往arguments里面添加一个5,这在ES6之前是不允许的; } show(1,2,3,4);
New Loop for of
for of 用来遍历(迭代/循环)整个对象;表现类似于for in; var arr = [‘banana‘,‘pear‘,‘orange‘,‘apple‘]; for(var i in arr){//i是索引; console.log(i);//0,1,2,3,索引; } for(var i of arr){//i是值; console.log(i);//banana,pear,orange,apple; }for of 循环不能用来循环json,可以循环数组,for of的真正目的是为了循环map对象;实例如下: var json = { ‘a‘:‘12‘, ‘b‘:‘11‘, ‘c‘:‘10‘, }; for(var name of json){ console.log(name);//json[Symbol.iterator] is not a function(…) } for of 也可以循环数组: 只循环值:for(var name of arr){ console.log(name) }; 只循环索引:for(var name of arr.keys()){ console.log(name) }; 索引和值都循环: for(var name of arr.entries()){ console.log(name) };
Map Object
Similar to JSON, it is also the form of key-value pairs key-value; The Map object is primarily intended to be used in conjunction with the for of loops. How to get a Map object: var oMap = new map (); settings: Omap.set (Name,value); Omap.set (Name,value);//Set values for the Map object; Omap.set (' A ', ' banana '); Omap.set (' B ', ' pear '); Console.log (OMAP);//{"a" = "banana", "B" and "Pear"} Get: Omap.get (name); Omap.get (' a ');//banana; Delete: Omap.delete (name); Omap.delete (' a '); loop Map with for of:var oMap = new Map (); Omap.set (' A ', ' banana '); Omap.set (' B ', ' pear '); Omap.set (' C ', ' orange '); Omap.set (' d ', ' Apple '); for (var name of OMap) {==> The essence of this notation is to loop the omap.entries (); Console.log (name);//The result is not only name, but also value; A,banana b,pear c,orange d,apple}==> This type of notation is equivalent to: for (var name in Omap.entries ()) {Console.log (name); }//In the form of Key-value: for (var [key,value] of OMap) {Console.log (key+ ': ' +vlaue);//a:banana b:pear C:orange D:ap PLE} only loops key for (var key of Omap.keys ()) {Console.log (key);//a,b,c,d} branch Loop value for (Var val of OM Ap.values ()) {Console.log (val);//banana,pear,orange,apple}
Arrow functions
ES6之前: function show(a,b,c){ return a+b+c }ES6:=> 注意: 箭头函数里面的this指的是window; 在箭头函数中arguments不能使用了;ES6:给默认值 var move=()=>{ console.log(obj);//undefined;因为obj未传参;有些时候不希望有undefined;这个时候需要给一个默认值; } move(); var move=(obj=‘参数必须传递‘)=>{ console.log(obj);//参数必须传递; }
2016/11/29 16:40:07
About array string methods for ES6