我是(全部)JavaScript中的新手(或者確切地說,在ES6中),並使用this article來指導我。我想問的是第二行。 type
在那個import語句中意味着什麼?是type
在JavaScript中的關鍵字?因爲如果正確理解它,我知道該行:import type { fromJS } from 'immutable'
,意思是從包immutable
(我來自Python背景)導入fromJS
函數。`type`在以下JavaScript代碼段中的含義是什麼?
我也看到在action: {type: string, payload: any }) => {
行中有type
參數。但我想這只是巧合吧?
import Immutable from 'immutable'
import type { fromJS } from 'immutable'
import { SAY_HELLO } from '../action/hello'
const initialState = Immutable.fromJS({
message: 'Initial message',
})
const helloReducer = (state: fromJS = initialState, action: {type: string, payload: any }) => {
switch(action.type) {
case SAY_HELLO:
return state.set('message', action.payload)
default:
return state
}
}
export default helloReducer
DD
下面的文章中的代碼片段段落解釋了什麼是'進口type'是爲了。 – 4castle
Aaaargh,明白了!這就是爲什麼我在代碼中感到困惑,因爲我沒有從JS重命名。如果您將答覆轉到答案部分,我會接受它作爲答案。 – swdev
Altough,@ 4castle,雖然有一個問題。 'fromJS'是函數,對不對?這是否意味着'type'會返回一個函數作爲'import type {fromJS}'的結果?或者,返回變量類型'fromJS'的類型是? – swdev