Javascript(ES6)const带花括号 [英] Javascript (ES6) const with curly braces
问题描述
const {
abc,
def
} = Object;
我已经搜索了Google和许多网站,解释了新的ES6规范。我知道这不是当前的实现,因为我的控制台输入时出现错误。
这段代码是什么意思?
更新
我将此代码段粘贴到 Babel's transpiler ,这是它返回的:
use strict;
var abc = Object.abc;
var def = Object.def;
我仍然感到困惑,这是为了完成这件事。
它是一个ES2015 解构任务。
它可能有助于看到它以更详细的方式重写。
const abc = Object.abc;
const def = Object.def;
这是从对象提取属性到变量的一种简洁的方法。
//你可以重写这个
const name = app.name;
const version = app.version;
const type = app.type;
// as
const {name,version,type} = app;
浏览器供应商是仍然实现了ES2015规范,这可能是为什么它在浏览器中不起作用。
但是,有一个名为 Babel 的项目可让您将未来的Javascript规范转换回来进入ES5。您可以在他们的REPL 中尝试ES2015代码。
I'm new to ECMAScript 6, and while trying to learn Ember, I've seen the following code style occassionally:
const {
abc,
def
} = Object;
I've searched Google and many sites explaining the new ES6 specifications. I know this is not the current implementation, because my console gives an error when I input that.
What does this code mean?
UPDATE
I pasted this snippet into Babel's transpiler, and this is what it returned:
"use strict";
var abc = Object.abc;
var def = Object.def;
I'm still confused as to what this is trying to accomplish.
It is an ES2015 destructuring assignment.
It might help to see it rewritten in a more verbose way.
const abc = Object.abc;
const def = Object.def;
It's a syntatically terse way of extracting properties from objects, into variables.
// you can rewrite this
const name = app.name;
const version = app.version;
const type = app.type;
// as this
const { name, version, type } = app;
Browser vendors are still implementing the ES2015 specification which is probably why it didn't work in your browser.
However, there's a project called Babel which allows you to convert future specifications of Javascript back into ES5. You can try out ES2015 code in their REPL.
这篇关于Javascript(ES6)const带花括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!