In coffeescript this is straightforward:
coffee> a = ['a', 'b', 'program']
[ 'a', 'b', 'program' ]
coffee> [_..., b] = a
[ 'a', 'b', 'program' ]
coffee> b
'program'
Does es6 allow for something similar?
> 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:UsersuserAppDataRoaming
pm
ode_modulesabel
ode_modulesabel-core
ode_modulesabylonlibparserlocation.js:24:13)
Of course I can do it the es5 way -
const a = b[b.length - 1]
But maybe this is a bit prone to off by one errors. Can the splat only be the last thing in the destructuring?
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…