我覺得我這樣做,因此我需要檢查的是,前述變量不是undefined
檢查下一個鏈中前:Javascript - 如何檢查父對象是否未定義?
if(this.props.someVar && this.props.someVar.data) {
// do something with this.props.someVar.data ...
}
這將是理想的只是這樣做:
if(this.props.someVar.data) {
// This throws an error because I haven't checked if `this.props.someVar` exists beforehand.
}
有沒有更容易/更短的方式來這麼做,最好使用純Javascript?
檢查'someVar'是否使用'!= undefined'語句未定義。 – Oen44
我知道,不是「純粹的」JS,但是使用[immutableJS](https://facebook.github.io/immutable-js/docs/#/)具有諸如[getIn](https ://facebook.github.io/immutable-js/docs/#/Collection/getIn) – thedude
嘗試使用'!!'與父母然後測試孩子 –