Skip to content

Instantly share code, notes, and snippets.

@rwaldron
Created June 1, 2012 00:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rwaldron/2847608 to your computer and use it in GitHub Desktop.
Save rwaldron/2847608 to your computer and use it in GitHub Desktop.
Function.prototype.isBound = function() {
return !this.prototype;
};
// Imaginary DOM library
var DOM = {};
// This function accepts a function as it's second argument,
// what happens when its a bound function? I'd like write the
// API in a way that is friendly to both types of function
DOM.each = function( elems, callbackFn ) {
var elem, length, k;
length = elems.length;
k = -1;
while ( ++k < length ) {
elem = elems[k];
if ( callbackFn.isBound() ) {
callbackFn( k, elem );
} else {
callbackFn.call( elem, k, elem );
}
}
}
let nodes = document.querySelectorAll("body");
DOM.each( nodes, function( index ) {
console.log(index, this);
});
function ListOfNodes( selector ) {
let nodes = document.querySelectorAll( selector );
// I don't want to return a NodeList, I'm
// making my own API - WOO!
DOM.each( nodes, index => {
// |this| is the constructed instance
this[ index ] = nodes[ index ];
});
this.length = nodes.length;
}
// I might make my own API for
// manipulating my ListOfNodes
// ListOfNodes.prototype.foo...
var list = new ListOfNodes("body");
function orig() {
console.log(this);
}
orig.prototype = {};
var bound = orig.bind({});
bound.isBound();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment