我只是面對這個Java Script代碼:JavaScript中,陣營常量
const {
myKey,
uname,
issorted,
title,
hClick,
} = this.props;
請告訴我這是什麼意思?它有什麼含義?
由於
我只是面對這個Java Script代碼:JavaScript中,陣營常量
const {
myKey,
uname,
issorted,
title,
hClick,
} = this.props;
請告訴我這是什麼意思?它有什麼含義?
由於
這是Destructuring Assignment一個例子。基本上它是從每個提供的對象鍵中聲明變量。您可以訪問像
console.log(uname)
這些變量,而不是
console.log(this.props.uname)
除此之外,如果你問「常量」爲好,this has a good explanation
這是一個對象destructuring assignment。在您的代碼示例中,它將this.props
對象中的數據抽取到分配左側聲明的不同常量(myKey
,uname
,,issorted
...)中。所以,如果this.props
是這樣一個對象:
this.props = {
myKey: 'value1',
uname: 'value2',
issorted: 'value3',
title: 'value4',
hClick: 'value5'
}
調用提到代碼的聲明的常量(myKey
,uname
,issorted
...)將有相應的對象屬性的值之後 - 例如myKey
常數將等於value1
。
在您的代碼示例const
告訴對象的數據將被提取到constants但當然你也可以使用與let
或var
聲明的變量。
在近期的Javascript版本中,解構已成爲訪問對象內某些值並對其進行本地化的流行方法。
在這種情況下,this.props
包含屬性myKey, uname, issorted, title, hClick
,可能包括其他屬性。爲了獲取特定屬性而不是複製整個對象,我們按照您提供的代碼所示的方式進行結構分配並根據其名稱對各個屬性進行本地化。