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
某草草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;
淡淡烟草味2017-05-19 10:40:56
var meta = {
auth: 33 // 把这个去掉你看看输出的值是什么?
};
// {auth = true} 是设置默认值。
var { auth = true } = meta; // meta 是一个对象, 如果auth没有, auth 等于 true
console.log(auth);
某草草2017-05-19 10:40:56
If written in es5, it is like this:
var auth = (typeof meta.auth!=='undefined')? meta.auth: true;
阿神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