search

Home  >  Q&A  >  body text

javascript - es6 object syntax

What does it mean

var { auth = true } = meta

It feels like {} is an object, but the format inside is not quite right. Shouldn’t it be { auth: true }, and then assign a value meta value? Don’t know what it means

PHP中文网PHP中文网2748 days ago573

reply all(6)I'll reply

  • 某草草

    某草草2017-05-19 10:40:56

    Here is object destructuring in ES6

    //对象的解构也可以指定默认值。
    var {x = 3} = {};
    x // 3
    
    var {x, y = 5} = {x: 1};
    x // 1
    y // 5
    
    var {x:y = 3} = {};
    y // 3
    
    var {x:y = 3} = {x: 5};
    y // 5
    
    var { message: msg = 'Something went wrong' } = {};
    msg // "Something went wrong"
    

    For this kind of problem, I hope you can check it later through the babeljs.io official online compiler

    // 上面的代码,可以这样理解
    var auth = meta.auth === undefined ? true : meta.auth;

    reply
    0
  • 習慣沉默

    習慣沉默2017-05-19 10:40:56

    Destructuring assignment.

    New features in es6.

    reply
    0
  • 迷茫

    迷茫2017-05-19 10:40:56

    This outside is destructuring assignment

    This is the default value

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-05-19 10:40:56

    var meta = {
        auth: 33   // 把这个去掉你看看输出的值是什么?
    };
    
    // {auth = true}  是设置默认值。
    var { auth = true } = meta;  // meta 是一个对象, 如果auth没有, auth 等于 true
    
    console.log(auth);

    reply
    0
  • 某草草

    某草草2017-05-19 10:40:56

    If written in es5, it is like this:

    var auth = (typeof meta.auth!=='undefined')? meta.auth: true;

    reply
    0
  • 阿神

    阿神2017-05-19 10:40:56

    Destructuring assignment in ES6

    { auth = true } It is equivalent to assigning a default value to auth. If the value of the auth attribute in the meta object is undefined, the default value true will be assigned to the variable auth

    reply
    0
  • Cancelreply