javascript变量名的意思是{花括号} [英] What do {curly braces} around javascript variable name mean

查看:1163
本文介绍了javascript变量名的意思是{花括号}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑
在查看JSHint后,我发现这个'解构表达式'在ES6(使用esnext选项)或Mozilla JS扩展(使用moz)中可用这个然而在阅读之后,我仍然不了解为什么使用



我在 MDN

  var ui = require(sdk / ui); 
var {ActionButton} = require(sdk / ui / button / action);

第二行的大括号是干什么用的?为什么在第一行没有花括号?

解决方案

这就是所谓的解构赋值,这是 JavaScript 1.7 (和

  var ActionButton = require(sdk / ui / button / action)。 

这个例子看起来很愚蠢,因为只有一个项目被分配。但是,您可以使用此模式一次分配多个变量:

  {x,y} = foo; 

相当于:

  x = foo.x;信息范讯卉范信辛

这也可以用于数组。例如,您可以轻松地交换两个值而不使用临时变量:

  var a = 1; 
var b = 3;

[a,b] = [b,a];






浏览器支持可以使用 kangax'ES6兼容性表格


EDIT After looking at JSHint I found this 'destructuring expression' is available in ES6 (use esnext option) or Mozilla JS extensions (use moz) and this however after reading it I still don't understand why it is used

I have come across the following code on MDN

var ui = require("sdk/ui");
var { ActionButton } = require("sdk/ui/button/action");

What do the braces on the second line do and why are they used? Why are there no braces on the first line?

解决方案

This is what's known as a destructuring assignment, and it's a new feature of JavaScript 1.7 (and ECMAScript 6) (Currently, only available in the FireFox JavaScript engine.) Roughly, it would translate into this:

var ActionButton = require("sdk/ui/button/action").ActionButton;

It seems silly in this example, as there's only one item being assigned. However, you'd be able to use this pattern to assign multiple variables at once:

{x, y} = foo;

Is the equivalent to:

x = foo.x;
y = foo.y;

This can also be used for arrays. For example, you could easily swap two values without using a temporary variable:

var a = 1;
var b = 3;

[a, b] = [b, a];


Browser support can be tracked using kangax' ES6 compatibility table.

这篇关于javascript变量名的意思是{花括号}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆