Last active
October 10, 2015 19:43
-
-
Save jczimm/37e1bcea13b82d9b056c to your computer and use it in GitHub Desktop.
Fancy array construction (fully extendable)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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