What is the significance of the dunder-bar naming? If it's numeric indices, then "get" and "set" would work just the same... unless I'm missing something?
Rick On Tue, Sep 18, 2012 at 2:41 PM, Nathan Rajlich <[email protected]>wrote: > Hey all, just wanted to throw out a new module of mine that I finished > up this weekend: array-index[0]. This module makes it easy to define > your own "array classes", that invoke custom accessor functions when > the numerical indexes are gotten or set. > > I personally needed this module to implement "ref-array", which will > be a module that lets you create arbitrary typed arrays backed by > Buffers using the "ref" interface. Ultimately this will be sweet when > using node-ffi. > > As a silly example, here's how you can invoke "Math.pow(2, index)" for > whatever index is accessed on the "array-index" instance: > > var ArrayIndex = require('array-index') > > // let's just create a singleton instance. > var a = new ArrayIndex() > > // the "__get__" function is invoked for each "a[n]" access. > a.__get__ = Math.pow.bind(Math, 2) > > // you must set the "length" on each instance manually > a.length = 10 > > console.log(a) > // [ 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, __get__: [Function] ] > > You can probably come up with some pretty sweet use-cases for this, > and let me know if you do! Cheers! > > [0]: https://github.com/TooTallNate/array-index > > -- > Job Board: http://jobs.nodejs.org/ > Posting guidelines: > https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines > You received this message because you are subscribed to the Google > Groups "nodejs" group. > To post to this group, send email to [email protected] > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group at > http://groups.google.com/group/nodejs?hl=en?hl=en > -- Job Board: http://jobs.nodejs.org/ Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines You received this message because you are subscribed to the Google Groups "nodejs" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en
