相关疑难解决方法(0)

解构以获取es6中数组的最后一个元素

在coffeescript中这很简单:

coffee> a = ['a', 'b', 'program']
[ 'a', 'b', 'program' ]
coffee> [_..., b] = a
[ 'a', 'b', 'program' ]
coffee> b
'program'
Run Code Online (Sandbox Code Playgroud)

es6是否允许类似的东西?

> const [, b] = [1, 2, 3]                              
'use strict'                                           
> b  // it got the second element, not the last one!                      
2                                                      
> const [...butLast, last] = [1, 2, 3]          
SyntaxError: repl: Unexpected token (1:17)                                                                                                                                                        
> 1 | const [...butLast, last] = [1, 2, 3]                                                                                                                                                        
    |                  ^                                                                                                                                                                          
    at Parser.pp.raise (C:\Users\user\AppData\Roaming\npm\node_modules\babel\node_modules\babel-core\node_modules\babylon\lib\parser\location.js:24:13)                                           
Run Code Online (Sandbox Code Playgroud)

当然我可以用es5方式做到 -

const …
Run Code Online (Sandbox Code Playgroud)

destructuring ecmascript-6

93
推荐指数
10
解决办法
6万
查看次数

标签 统计

destructuring ×1

ecmascript-6 ×1