一 . let和const都是塊級作用域
ES6以前, var關(guān)鍵字聲明變量. 無論聲明在何處, 都會被視為聲明在函數(shù)的最頂部(不在函數(shù)內(nèi)即在全局作用域的最頂部).
var 為全局變量 或者函數(shù)全局變量.
ES6中 let 表示變量、const 表示常量, let和const都是塊級作用域, 說白了 {}大括號內(nèi)的代碼塊即為let 和 const的作用域
打印0~9可以這樣寫了
for (let i = 0; i < 10; i++) {
func.push(function() {
console.log(i)
})
}
funcs.forEach(function(func) {
func()
})
二. ES6反引號(``)搞定換行, 推薦使用
// es6
const t = `<h3>
<span>hello world</span>
<h3>`
三. 箭頭函數(shù)
箭頭函數(shù)特點(diǎn)
- 創(chuàng)建函數(shù)不需要function關(guān)鍵字
- 省略return
- 繼承當(dāng)前上下文的 this 關(guān)鍵字
//例如:
[1,2,3].map( x => x + 1 )
//等同于:
[1,2,3].map((function(x){
return x + 1
}).bind(this))
四. 對象添加方法
ES6省略冒號與 function 關(guān)鍵字,簡潔
render(){
var text = this.state.liked? ' like ':' don\'t like ';
return (
<div>
<button onClick={this.handleClick}>you {text} it, click to change it!</button>
</div>
)
};