不寻常的 javascript 对象表示法

Unusual javascript object notation

以下代码中使用的 _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'
    });
});

被称为Destructuring Assignment. Basically, req.user is an object with keys _raw, _json and other keys. With that syntax, you are reading directly the properties _raw and _json of the object and the rest of the object is saved into the userProfile variable. For that part, the Spread Syntax的符号被使用。

演示示例:

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