Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
69 lines (49 sloc) 2.13 KB

Extensible Destructuring


Programmers use Objects for the purpose of String keyed hash maps. On some occasions, Objects are not well suited as map like data structure and that is the reason why alternative datastructures like ECMAScript 2015 Map, or [ImmutableJS Map]( ImmutableJS Map) exist and get used.

Object destructuring has become part of ECMAScript 2015.

Currently programmers who use alternative map like data structures instead of Object are out of luck. They can't do destructuring on these data structures which results in very verbose code:


const {author: {name: {first, last}, birthdate}} = book;


const first = book.get('author').get('name').get('first')
const last = book.get('author').get('name').get('last')
const birthdate = book.get('author').get('birthdate')


Let's add Symbol.get (@@get). Then add following to Object.prototype

Object.prototype[Symbol.get] = (key) => this[key]

Whenever object destructuring is used, @@get method is used to extract value of keys.

Then destructuring example from our previous example would be equivalent to

const first = book[Symbol.get]('author')[Symbol.get]('name')[Symbol.get]('first')
const last = book[Symbol.get]('author')[Symbol.get]('name')[Symbol.get]('last')
const birthdate = book[Symbol.get]('author')[Symbol.get]('birthdate')

Destructuring of non-string keys

Object support only string keys, but other data structures could support more than that. To allow using non-string keys in destructuring assignment whilst preventing ambiguity, following syntax can be reused:

// Demonstration on immutable data structures, because they support compound
// keys.
import {Map, List} from 'immutable';

const map = Map([[List(20, 10, 1989), Map([['weekday', 'sunday']])]]);
const {[List(20, 10, 1989)]: {weekday}} = map;

Implementation by transpilers

There is babel plugin for that: