简体   繁体   English

这些代码在解构对象中是否有任何功能差异?

[英]Is there any functional difference between these code in destructuring object?

While I'm using destructuring assignment to assign variables from arrays, I find another way of destructing.当我使用解构赋值从数组中分配变量时,我找到了另一种解构方法。 Is there any difference between these two codes?这两个代码有什么区别吗?

This is a challenge from freeCodeCamp using javascript, ES6.这是来自 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));

There will be a difference only if it requires default values只有需要默认值才会有区别

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM