immutable-js/immutable-js

Use ES6 Proxies where available for indexed access on Indexed

Open

#440 创建于 2015年4月9日

在 GitHub 查看
 (20 评论) (16 反应) (0 负责人)TypeScript (33,046 star) (1,774 fork)batch import
enhancementhelp wanted

描述

list.get(index) syntax works fine some of the time but it adds up a learning curve + migration cost when you wish swap out arrays in the existing code with lists. I think it would be very nice to put the Proxy like following at the bottom of the prototype chain to allow optional access by index:

let accesor = new Proxy(Object.prototype, {
  get(target, property, receiver) {
    return receiver.get(property)
  }
});

// Workaround to monkey patch loaded immutable.js library, although it should be:
// Iterable.prototype = Object.create(accessor, {....})
Object.setPrototypeOf(Immutable.Iterable.prototype, accesor)

Immutable.List.of(1, 2, 3)[0] // => 1
Immutable.List.of(1, 2, 3)[1] // => 2
Immutable.List.of(1, 2, 3)[5] // => undefined
Immutable.List.of(1, 2, 3)[-2] // => 2

贡献者指南

Use ES6 Proxies where available for indexed access on Indexed · immutable-js/immutable-js#440 | Good First Issue