1,let 聲明變量
let 聲明的變量只能在let 的塊級作用域中生效,也是為了彌補var聲明變量的全局污染問題。
var 聲明變量有變量提升的作用,也就是在聲明變量之前可以使用變量
console.log(x);
var x = 10;
不會報錯。而let會報錯。
let 不允許在同一塊作用域下聲明同一個變量,而var 可以。
function foo(){
let x = 10;
var x = 20;
}會報錯;
或者function foo (){
let x = 10;
let x = 20;
}也會報錯。
ES5中只有全局作用域和函數作用域,沒有塊級作用域。
var name = '' naza"
function foo(){
console.log(name)
if (false){
var name = 'NN'
}
}
foo()
undifined
var name = 'Q1mi'
function foo(){
console.log(name)
if (false){
let name = 'Bob'
}
}
foo() // Q1mi
此時,在foo函數內容,外層代碼塊就不再受內層代碼塊的影響。所以類似for循環的計數變量我們最好都是用let來聲明。
2,const 聲明常量
const聲明常量,常量必須立即初始化,常量不可更改。
const 和let一樣, 只在所在的塊級作用域中有效。
ES6規定:var命令和function命令聲明的全局變量依舊是全局對象的屬性;let命令、const命令和class命令聲明的全局變量不屬於全局對象的屬性。
var 和function聲明的變量默認會在window對象上,
let 和 const ,申明的所有變量不會出現在window中。
3,變量的解構賦值
ES6允許按照一定的模式,從數組或對象中提取值,對變量進行賦值,這種方式被稱為解構賦值。
var [x,y,z] = [10,20,30]
對象的結構賦值
var {x, y} = {x:21, y:30}
4,字符串
在此之前,JavaScript中只有indexOf方法可用來確定一個字符串是否包含在另一個字符串中。
include、startsWith、endsWith
ES6中又提供了3種新方法:
includes():返回布爾值,表示是否找到了參數字符串。
startsWith():返回布爾值,表示參數字符串是否在源字符串的開始位置。
endsWith():返回布爾值,表示參數字符串是否在源字符串的結尾位置。
var s = 'hello world' s.includes('hello'), true s.startsWith("hello"),true endsWith('world') 后面還可以加參數 s.includes('hello',3), 可以加索引 s.startsWith("hello",3),可以加索引 endsWith('world',4)可以加索引
5,模板字符串
模板字符串(template string)是增強版的字符串,用反引號(`)標識。它可以當做普通字符串使用,也可以用來定義多行字符串,或者在字符串中嵌入變量。在模板字符串中嵌入變量,需要將變量名寫入${}中。
var name = 'yuyu' var age = 18 `my name is ${name}, I'm age is ${age} years old`
6,函數
箭頭函數
箭頭函數有個特點:
- 如果參數只有一個,可以省略小括號
- 如果不寫return,可以不寫大括號
- 沒有arguments變量
- 不改變this指向
其中箭頭函數中this指向被固定化,不是因為箭頭函數內部有綁定this的機制。實際原因是箭頭函數根本沒有自己的this,導致內部的this就是外層代碼塊的this。
var person = { name: 'qimi', age: 18, func:function(){ console.log(this); } } person.func() VM44:5 {name: "qimi", age: 18, func: ƒ}
var person = { name: 'qimi', age: 18, func:()=>{ console.log(this); } } person.func() VM48:5 Window {postMessage: ƒ, blur: ƒ, focus: ƒ, close: ƒ, frames: Window, …}
7,對象
ES6允許直接寫入變量和函數作為對象的屬性和方法。
屬性的簡潔表示法
function (x, y){ return {x, y} }
等同於
function (x, y){ return {x:x, y:y} }
對象的方法也可以使用簡潔表示法:
var o = { method(){ return "hello!"; } }
等同於
var o = { method:function(){ return "hello" } }
8,Object.assign()
Object.assign方法用來將源對象(source)的所有可枚舉屬性復制到目標對象(target)。它至少需要兩個對象作為參數,第一個參數是目標對象,第二個參數是源對象。
參數必須都是對象,否則拋出TypeError錯誤。
Object.assjgn只復制自身屬性,不可枚舉屬性(enumerable為false)和繼承的屬性不會被復制。
var x = {name: "Q1mi", age: 18}; var y = x; var z = Object.assign({}, x); x.age = 20; x.age // 20 y.age // 20 z.age // 18
9,面向對象
ES5的構造對象的方式 使用構造函數來創造。構造函數唯一的不同是函數名首字母要大寫。
function Point(x, y){ this.x = x; this.y = y; } // 給父級綁定方法 Point.prototype.toSting = function(){ return '(' + this.x + ',' + this.y + ')'; } var p = new Point(10, 20); console.log(p.x) p.toSting(); // 繼承 function ColorPoint(x, y, color){ Point.call(this, x, y); this.color = color; } // 繼承父類的方法 ColorPoint.prototype = Object.create(Point.prototype); // 修復 constructor ColorPoint.prototype.constructor = Point; // 擴展方法 ColorPoint.prototype.showColor = function(){ console.log('My color is ' + this.color); } var cp = new ColorPoint(10, 20, "red"); console.log(cp.x); console.log(cp.toSting()); cp.showColor()
ES6 使用Class構造對象的方式:
class Point{ constructor(x, y){ this.x = x; this.y = y; } // 不要加逗號 toSting(){ return `(${this.x}, ${this.y})`; } } var p = new Point(10, 20); console.log(p.x) p.toSting(); class ColorPoint extends Point{ constructor(x, y, color){ super(x, y); // 調用父類的constructor(x, y) this.color = color; } // 不要加逗號 showColor(){ console.log('My color is ' + this.color); } } var cp = new ColorPoint(10, 20, "red"); console.log(cp.x); cp.toSting(); cp.showColor()
10,Promise
Promise 是異步編程的一種解決方案,比傳統的解決方案(回調函數和事件)更合理、更強大。它由社區最早提出和實現,ES6 將其寫進了語言標准,統一了用法,原生提供了Promise
對象。
使用Promise的優勢是有了Promise
對象,就可以將異步操作以同步操作的流程表達出來,避免了層層嵌套的回調函數。此外,Promise
對象提供統一的接口,使得控制異步操作更加容易。
用法示例:
const promiseObj = new Promise(function(resolve, reject) { // ... some code if (/* 異步操作成功 */){ resolve(value); } else { reject(error); } });
Promise
構造函數接受一個函數作為參數,該函數的兩個參數分別是resolve
和reject
。它們是兩個函數,由 JavaScript 引擎提供,不用自己部署。
Promise
實例生成以后,可以用then
方法分別指定resolved
狀態和rejected
狀態的回調函數。
promiseObj.then(function(value) { // success }, function(error) { // failure });
then
方法可以接受兩個回調函數作為參數。第一個回調函數是Promise
對象的狀態變為resolved
時調用,第二個回調函數是Promise
對象的狀態變為rejected
時調用。其中,第二個函數是可選的,不一定要提供。這兩個函數都接受Promise
對象傳出的值作為參數。
我們還可以將上面的代碼寫成下面這種方式:
promiseObj .then(function(value) { // success }) .catch(function(error) { // failure });
其實Promise.prototype.catch
方法是.then(null, rejection)
的別名,用於指定發生錯誤時的回調函數。