Skip to content

Instantly share code, notes, and snippets.

@jczimm
Last active October 10, 2015 19:43
Show Gist options
  • Save jczimm/37e1bcea13b82d9b056c to your computer and use it in GitHub Desktop.
Save jczimm/37e1bcea13b82d9b056c to your computer and use it in GitHub Desktop.
Fancy array construction (fully extendable)
function Chain (datum) {
if (datum != undefined) {
this.length = this.length !== undefined ? this.length + 1 : 1;
this[this.length - 1] = datum;
return Chain.bind(this);
} else {
return this;
}
};
Chain.prototype = Array.prototype;
var chain = new Chain("string")(33)(function getItem(i) {
// `this` is bound as chain.data
return this[i];
})();
console.log(chain);
// => ["string", 33, function function]
console.log(chain[2](1));
// => 33
chain.forEach(function(link) {
console.log(link);
});
// => string
// 33
// function getItem(i) { ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment