Created
October 12, 2011 19:06
-
-
Save angus-c/1282205 to your computer and use it in GitHub Desktop.
array-like NodeList via mixins
This file contains hidden or 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 NodeList = function() {}; | |
| var nodeListExtras = {x: 23}; | |
| mixin(NodeList.prototype, Array.prototype, nodeListExtras); | |
| function mixin(obj /*, mixins*/) { | |
| var mixins = [].slice.call(arguments, 1); | |
| for (var i=0; i<mixins.length; i++) { | |
| var thisMixin = mixins[i]; | |
| var props = Object.getOwnPropertyNames(thisMixin); | |
| for (var j=0; j<props.length; j++) { | |
| obj[props[j]] = thisMixin[props[j]]; | |
| } | |
| } | |
| return obj; | |
| } | |
| var nodeList = new NodeList(); | |
| console.log(nodeList instanceof NodeList); //true | |
| console.log(nodeList instanceof Array); //false | |
| console.log(nodeList.push); //native function | |
| console.log(nodeList.x); //23 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment