Created
June 1, 2012 00:38
-
-
Save rwaldron/2847608 to your computer and use it in GitHub Desktop.
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.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