Skip to content

Instantly share code, notes, and snippets.

@angus-c
Created October 12, 2011 19:06
Show Gist options
  • Select an option

  • Save angus-c/1282205 to your computer and use it in GitHub Desktop.

Select an option

Save angus-c/1282205 to your computer and use it in GitHub Desktop.
array-like NodeList via mixins
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