javascript - es6 object syntax
PHP中文网
PHP中文网 2017-05-19 10:38:56
0
6
598

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中文网

认证0级讲师

reply all(6)
某草草

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;
習慣沉默

Destructuring assignment.

New features in es6.

迷茫

This outside is destructuring assignment

This is the default value

淡淡烟草味
var meta = {
    auth: 33   // 把这个去掉你看看输出的值是什么?
};

// {auth = true}  是设置默认值。
var { auth = true } = meta;  // meta 是一个对象, 如果auth没有, auth 等于 true

console.log(auth);
某草草

If written in es5, it is like this:

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

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template