1妈拌、定義變量加入了 let const
????????let 會將變量提升到塊頂部(而非像ES5是函數(shù)頂部)北启。
????????但是在變量聲明前引用變量會造成錯誤恋腕。
????????let 是塊作用域的菲盾,不可以在聲明前使用磺芭。
????????const? 如果不希望變量的值在發(fā)生改變婴噩,使用const聲明變量骆撇。
2瞒御、封閉空間
? ? ? ? 為了避免污染全局環(huán)境污染不必再使用封閉空間,在局部環(huán)境中使用let來定義變量就解決了所有問題
????{? ? ? ? let? a=5;? ? }
? ? alert(a);? //undefined
3艾船、字符串和變量的拼接
????????1》單行字符串和變量的拼接
????????字符串和變量的拼接葵腹,在拼接的整個作用域內(nèi)加``(就是鍵盤1前面那個鍵的反引號),變量和字符串都不再加''屿岂,變量使用${變量名}践宴;
????????constname=jason;
? ? ? ? constage=18;
? ? ? ? alert(`His name is ${name},He is ${age}`);
2》多行字符串
????constoLi=`<li>
? ? ? ? ? <div>我是ES6</div>
? ? ? ? ? <p>這是我的拼接</p>
? ? ?</li>`
4、解構(gòu)賦值
????????1》數(shù)組的操作
constarr=[1,2,3,4,5];
?const[s,,,,n]=arr;
? alert(s,n);
????????2》值的對調(diào):例如冒泡排序中的數(shù)組的值的對調(diào)
????????function BubbleSort(arr){
? ? ? ? ? ? ? ? for(var i=0;i<arr.length;i++){
? ? ? ? ? ? ? ? ? ? for(var j=0;j<arr.length-1;j++){
? ? ? ? ? ? ? ? ? ? ? ? if(arr[j]>arr[j+1]){
? ? ? ? ? ? ? ? ? ? ? ? ? ? [arr[j],arr[j+1]]=[arr[j+1],arr[j]];
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? return arr;
? ? ? ? ? ? }
????????3》查找返回值(包含多個值的時候)
????????function obj(){
? ? ? ? ? ? ? ? constleft=1, right=2, top=3, bottom=4;
? ? ? ? ? ? ? ? return? ? {left,right,top,bottom};
? ? ? ? ? ? }
? ? ? ? ? ? const{left,bottom}=obj();
? ? ? ? ? ? console.log(left, bottom); // 1 4
5爷怀、類和面向?qū)ο?/p>
ES6中引入了類(class)來代替構(gòu)造函數(shù)(constructor)
? ??1》面向?qū)ο螅篍S6中引入了類(class)
????????class Person {
? ? ? ? ? ? ? constructor(name) {
? ? ? ? ? ? ? ? this.name = name;
? ? ? ? ? ? ? }
? ? ? ? ? ? ? showName() {
? ? ? ? ? ? ? ? return this.name;
? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? const P1 = new Person('jason');
? ? ? ? ? ? alert(P1.showName()); //jason
????????2》繼承:提供了新的關(guān)鍵字 extends 和 super
????????class Person {
? ? ? ? ? ? ? ? constructor(name,age) {
? ? ? ? ? ? ? ? ? this.name = name;
? ? ? ? ? ? ? ? ? this.age=age;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? showName() {
? ? ? ? ? ? ? ? ? return this.name;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? showAge(){
? ? ? ? ? ? ? ? ? return this.age;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? class Worker extends Person {
? ? ? ? ? ? ? ? ? constructor(name,age,job){
? ? ? ? ? ? ? ? ? ? ? super(name,age);
? ? ? ? ? ? ? ? ? ? ? this.job=job;
? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? showJob(){
? ? ? ? ? ? ? ? ? ? ? return this.job;
? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? const W1 = new Worker('jason',18,'要飯的');
? ? ? ? ? ? alert(W1.showJob()); // 要飯的