ES6
参考文章:
查看对es6语法的支持
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 D :\desktop\Vue > npm install -g es-checkeradded 13 packages in 4s PS D :\desktop\Vue > es-checkerECMAScript 6 Feature Detection (v1.4 .2 )Variables √ let and const √ TDZ error for too-early access of let or const declarations √ Redefinition of const declarations not allowed √ destructuring assignments/declarations for arrays and objects √ ... operator Data Types √ For ...of loop √ Map , Set , WeakMap , WeakSet √ Symbol √ Symbols cannot be implicitly coerced Number √ Octal (e.g . 0o1 ) and binary (e.g . 0b10 ) literal forms √ Old octal literal invalid now (e.g . 01 ) √ Static functions added to Math (e.g . Math .hypot (), Math .acosh (), Math .imul () ) √ Static functions added to Number (Number .isNaN (), Number .isInteger () ) String √ Methods added to String .prototype (String .prototype .includes (), String .prototype .repeat () ) √ Unicode code-point escape form in string literals (e.g . \u{20BB7} ) √ Unicode code-point escape form in identifier names (e.g . var \u{20BB7} = 42 ; ) √ Unicode code-point escape form in regular expressions (e.g . var regexp = /\u{20BB7}/u ; ) √ y flag for sticky regular expressions (e.g . /b/y ) √ Template String Literals Function √ arrow function √ default function parameter values √ destructuring for function parameters √ Inferences for function name property for anonymous functions × Tail -call optimization for function calls and recursion Array √ Methods added to Array .prototype ([].fill (), [].find (), [].findIndex (), [].entries (), [].keys (), [].values () ) √ Static functions added to Array (Array .from (), Array .of () ) √ TypedArrays like Uint8Array , ArrayBuffer , Int8Array (), Int32Array (), Float64Array () √ Some Array methods (e.g . Int8Array .prototype .slice (), Int8Array .prototype .join (), Int8Array .prototype .forEach () ) added to the TypedArray prototypes √ Some Array statics (e.g . Uint32Array .from (), Uint32Array .of () ) added to the TypedArray constructors Object √ __proto__ in object literal definition sets [[Prototype ]] link √ Static functions added to Object (Object .getOwnPropertySymbols (), Object .assign () ) √ Object Literal Computed Property √ Object Literal Property Shorthands √ Proxies √ Reflect Generator and Promise √ Generator function √ Promises Class √ Class √ super allowed in object methods √ class ABC extends Array { .. } Module × Module export command × Module import command ========================================= Passes 39 feature Detections Your runtime supports 92 % of ECMAScript 6 =========================================
基础语法
var,let,const
在能实现相同的功能情况下, 使用优先级: const > let > var
1 2 3 4 5 6 7 8 9 10 11 12 13 const str1 = "const定义" ;console .log (str1);const obj = { name : "Tiam" , age : 18 } obj.name = "字节跳动" ; obj.age = 9 ; console .log (obj);
模板字符串 | 字符串遍历
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 let str = "妖怪" ;console .log (`${str} 看打` );let string = "abcdefghijklmnopqrstuvwxyz" ;for (const c in string) { console .log (c); } for (const c of string) { console .log (c); } const str2 = "PUA" ;let [a, b, c] = str2;console .log (a, b, c);console .log (str2[1 ]);console .log (str2.includes ("A" ));console .log (str2.repeat (2 ));
set, map, object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 let obj_name, obj_age;let obj1 = { obj_name, obj_age, obj_func ( ) { console .log (this .obj_name ); } } obj1.obj_name = "对象名" obj1.obj_func (); let set = new Set ();set.add ("one" ); set.add ("two" ); set.add ("one" ); console .log (set); set.forEach (i => console .log (i)) let map = new Map ();map.set ("邓紫棋" , "《多远都要在一起》" ).set ("周杰伦" , "《明明就》" ); console .log (map);
rest参数 | 函数参数默认值 | 箭头函数 | 扩展运算符
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 function fun (name = "Tiam" , age = 18 ) { console .log (`${name} , ${age} ` ); } fun ();fun ("字节" , 102 );let fun1 = (...songs ) => console .log (songs);fun1 ("泡沫" , "再见" , "A.N.I.Y" , "光年之外" );function fun2 ( ){ console .log (arguments ); console .log (arguments .length ); console .log (...arguments ); } fun2 (1 ,2 ,3 ,4 ,5 );let obj = { "壹" : 1 , "贰" : 2 , "叁" : 3 } console .log ({...obj});
Class类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 class Person { constructor (name, age ) { this .name = name; this .age = age; } getName ( ) { return this .name ; } getAge ( ) { return this .age ; } setName (name ) { this .name = name; } setAge (age ) { this .age = age; } toString ( ) { return this .name + '(' + this .age + ')' ; } run ( ) { console .log (`Just run!${this .name} ` ); } static cry ( ) { console .log ("so sad, so cry!" ); } } console .log (typeof Person ); Person .cry ();let scientist = new Person ("爱因斯坦" , 77 );console .log (scientist);console .log (scientist.toString ());let mathematician = new Person ();mathematician.setName ("Oula" ); mathematician.setAge (98 ); console .log (mathematician.getName ());console .log (mathematician.getAge ());class Student extends Person { constructor (name, age, college ) { super (name, age); this .college = college; } getCollege ( ) { return this .college } } let linux_father = new Student ("林纳斯" , 21 , "芬兰大学" );console .log (linux_father.getCollege ); console .log (linux_father.getCollege ()); console .log (linux_father); Student .cry ();
let、const、var 的区别
var:使用 var 声明的变量,其作用域为该语句所在的函数内,且存在变量提升现象
let:使用 let 声明的变量,其作用域为该语句所在的代码块内,不存在变量提升。
const:使用 const 声明的是常量,在后面出现的代码中不能再修改该常量的值。
var
let
const
函数级作用域
块级作用域
块级作用域
变量提升
不存在变量提升
不存在变量提升
值可以改变
值可以改变
值不可以改变
1.关于let
1 2 3 4 5 6 7 8 let arr = [];for (let i = 0 ; i < 2 ; i++) { arr[i] = function ( ) { console .log (i); } } arr[0 ](); arr[1 ]();
2.关于const
1 2 3 const PI = 3.14 ; PI = 100 ;
1 2 3 4 5 const ary = [100 , 200 ];ary[0 ] = 'a' ; ary[1 ] = 'b' ; console.log (ary); ary = ['a' , 'b' ];
3.关于var
var关键字声明的变量,无论实际声明的位置在哪,都会被视为声明在函数的顶部、如果声明不在任意函数内,则视为在全局作用域的顶部
ES模块导入规则
*容易出错的地方*
页面不基于服务器运行,会出现跨域的错误
origin ‘null’ has been blocked by CORS policy: Cross origin requests are only
使用模块化时,页面不加type = “module” 会出现语法错误
app.js:1 Uncaught SyntaxError: Unexpected token {
1 <script src="./module/app.js" type="module" ></script>
import导入模块时不添加 .js 的后缀名会报找不到module错误
GET xxx net::ERR_ABORTED 404 (Not Found)
1 import { Student } from './Student.js' ;
导入导出方式
导出方式
定义时导出
批量导出
导出重命名(不建议)
默认导出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 export let uname = '李四' ;export function showStudentName ( ){ console .log (uname); } export class SomeAnimalInfo { constructor (type,age ){ this .type = type; this .age = age; } showInfo ( ){ console .log (`物种:${this .type} ,年龄:${this .age} ` ); } } const PI = 3.1415926 ;const DBNAME = 'Local' ;... ... export { PI , DBNAME };export default class { static log (msg ) { let now = new Date (); console .log (`${now.toLocaleString()} ${msg} ` ); } static setCookie ( ){ } ... ... }
导入方式
1 2 3 4 5 6 7 8 import {num, showStudentName as showName} from './all.js' ;import * as cons from './const.js' ;import Tool from './tools.js' ;
Symbol
ES6引入了一种新的原始数据类型Symbol,表示独一无二的值。它是JavaScript语言的第七种数据类型,前六种是:Undefined、Null、布尔值(Boolean)、字符串(String)、数值(Number)、对象(Object)
Symbol
null
undefined
boolean
string
number
object
1 2 3 4 5 6 7 8 9 10 11 let str1 = "邓紫棋" ;let str2 = "邓紫棋" ;console .log (str1); console .log (typeof str1); console .log (str1 == str2); let str3 = Symbol ("邓紫棋" );let str4 = Symbol ("邓紫棋" );console .log (str3); console .log (typeof str3); console .log (str3 == str4);
Promise对象
Promise是异步编程的一种解决方案
简单说就是一个容器,里面保存着某个未来才会结束的事件(通常是一个异步操作)的结果。
从语法上说,Promise是一个对象,从它可以获取异步操作的消息。
Promise提供统一的API,各种异步操作都可以用同样的方法进行处理。
ES6规定,Promise对象是一个构造函数,用来生成Promise实例。
http://caibaojian.com/es6/promise.html
进制表示
1 2 3 4 5 console .log (0B101 == 5 ); console .log (0O777 );
Array.from
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 let map = new Map ();map.set ("邓紫棋" , "《多远都要在一起》" ).set ("周杰伦" , "《明明就》" ); let arr1 = Array .from (map);console .log (arr1);let set = new Set ();set.add ("one" ).add ("two" ).add ("three" ); let arr2 = Array .from (set);console .log (arr2);const arrayLike = { 0 : "Tiam" , 1 : 18 , 2 : "hbnu" , length : 3 } let arr3 = Array .from (arrayLike);console .log (arr3);let arr4 = Array .of (1 , "2" , '3' , true , set, map);console .log (arr4);let fun = function ( ) { }console .log (fun.name );
严格模式
1 2 3 4 5 function doSomething (a, b ) { 'use strict' ; }
箭头函数
箭头函数有几个使用注意点。
(1)函数体内的this对象,就是定义时所在的对象,而不是使用时所在的对象。
(2)不可以当作构造函数,也就是说,不可以使用new命令,否则会抛出一个错误。
(3)不可以使用arguments对象,该对象在函数体内不存在。如果要用,可以用Rest参数代替。
(4)不可以使用yield命令,因此箭头函数不能用作Generator函数。
上面四点中,第一点尤其值得注意。this对象的指向是可变的,但是在箭头函数中,它是固定的 。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 let pow2 = param => param * param;console .log (pow2 (9 ));let fun1 = ( ) => { console .log ("操作系统" ); console .log ("数据结构" ); } fun1 ();let fun2 = (song1,song2 )=>{ console .log (`${song1} 很好听` ); return song2; } console .log (fun2 ("where are you" , "kiss the rain" ));
Generator 函数
Generator函数是分段执行的,yield语句是暂停执行的标记,而next方法可以恢复执行。
Generator函数是一个状态机,封装了多个内部状态。
执行Generator函数会返回一个遍历器对象,也就是说,Generator函数除了状态机,还是一个遍历器对象生成函数
function关键字与函数名之间有一个星号;
函数体内部使用yield语句,定义不同的内部状态
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 function * hwg ( ) { yield "Hello" ; yield "World" ; return "end" ; } let generatorFun = hwg ();for (let i = 0 ; i < 4 ; i++) { console .log (generatorFun.next ()); }