这些代码在解构对象中有什么功能上的区别吗?
Is there any functional difference between these code in destructuring object?
当我使用解构赋值从数组中赋值变量时,我找到了另一种解构方法。这两个代码有什么区别吗?
这是来自 freeCodeCamp 的挑战,使用 javascript、ES6。
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));
只有需要默认值才会有区别
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;
}
当我使用解构赋值从数组中赋值变量时,我找到了另一种解构方法。这两个代码有什么区别吗?
这是来自 freeCodeCamp 的挑战,使用 javascript、ES6。
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));
只有需要默认值才会有区别
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;
}