Необычная нотация объектов javascript - PullRequest
0 голосов
/ 28 февраля 2019

Что означают _raw, _json и ...userProfile, используемые в приведенном ниже коде?Это из примера auth0.Спасибо!

router.get('/user', secured(), function (req, res, next)
{
    const { _raw, _json, ...userProfile } = req.user;
    console.log ('rec user ', req.user);
    //console.log ('user profile ', userProfile);
    res.render('user', {
        userProfile: JSON.stringify(userProfile, null, 2),
        title: 'Profile page'
    });
});

1 Ответ

0 голосов
/ 28 февраля 2019

Это обозначение называется Назначение деструктуры .По сути, req.user - это object с клавишами _raw, _json и другими клавишами.С этим синтаксисом вы читаете непосредственно свойства _raw и _json объекта, а остальная часть объекта сохраняется в переменной userProfile.Для этой части используется синтаксис Spread .

Демонстрационный пример:

const req = {
    user: {
      _raw: "raw",
      _json: "json",
      other1: "other1",
      other2: "other2"
    } 
};

const { _raw, _json, ...userProfile } = req.user;
console.log("_raw is: ", _raw);
console.log("_json is: ", _json);
console.log("userProfile is: ", userProfile);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}
...