2016-02-29 30 views
3

我開始接觸反應,我不知道爲什麼他們選擇使用這個符號:爲什麼React使用函數作爲變量?

const counter = (state, action) => {}

而不是舊的傳統:

function counter(state, action){}

我想知道是否因爲const關鍵字。因爲它不可能創建一個const function它(如上所述:Are there constants in JavaScript?

是唯一的原因嗎?我明白const函數在React中很重要,以確保在運行時不會改變行爲。但我想知道這是他們選擇使用它的唯一原因。

回答

2

這是個人的選擇,它是新的名爲arrow functionES2015功能,

const counter = (state, action) => { } 

// you can also define counter like this 
const counter = function (state, action) { } 

的主要區別是,arrow function沒有自己的thisarguments

相關問題