虽然我正在使用分解分配从数组中分配变量,但我发现了另一种分解方法。这两个代码之间有什么区别吗?
这是使用JavaScript,ES6的freeCodeCamp的挑战。
const LOCAL_FORECAST = {
today: { min: 72, max: 83 },
tomorrow: { min: 73.3, max: 84.6 }
};
function get_MaxOfTmrw(forecast)
{
const {tomorrow:{max : max_OfTomorrow}} = forecast;
return max_OfTomorrow;
}
function get_MaxOfTmrw(forecast)
{
const {max : max_OfTomorrow}} = forecast.tomorrow;
return max_OfTomorrow;
}
console.log(get_MaxOfTmrw(LOCAL_FORECAST));
答案 0 :(得分:1)
只有在需要默认值的情况下才会有所不同
function get_MaxOfTmrw(forecast)
{
const { tomorrow: { max : max_OfTomorrow = 0 } = {} } = forecast
return max_OfTomorrow
}
function get_MaxOfTmrw(forecast)
{
const { max : max_OfTomorrow = 0 } = forecast.tomorrow; // what if tomorrow is undefined?
return max_OfTomorrow;
}