Created
December 10, 2010 22:18
-
-
Save jashkenas/736920 to your computer and use it in GitHub Desktop.
Snippet from Zepto.js, translated to CoffeeScript.
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 adjacencyOperators = { | |
append: 'beforeEnd', | |
prepend: 'afterBegin', | |
before: 'beforeBegin', | |
after: 'afterEnd' | |
}; | |
for (key in adjacencyOperators) | |
$.fn[key] = (function(operator) { | |
return function(html){ | |
return this.each(function(element){ | |
element['insertAdjacent' + (html instanceof Element ? 'Element' : 'HTML')](operator, html); | |
}); | |
}; | |
})(adjacencyOperators[key]); |
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
adjacencyOperators = | |
append: 'beforeEnd' | |
prepend: 'afterBegin' | |
before: 'beforeBegin' | |
after: 'afterEnd' | |
for key of adjacencyOperators | |
$.fn[key] = (html) -> | |
@each (element) -> | |
element['insertAdjacent' + (if html instanceof Element then 'Element' else 'HTML')] operator, html |
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 adjacencyOperators, _fn; | |
adjacencyOperators = { | |
append: 'beforeEnd', | |
prepend: 'afterBegin', | |
before: 'beforeBegin', | |
after: 'afterEnd' | |
}; | |
_fn = function(key) { | |
return $.fn[key] = function(html) { | |
return this.each(function(element) { | |
return element['insertAdjacent' + (html instanceof Element ? 'Element' : 'HTML')](operator, html); | |
}); | |
}; | |
}; | |
for (key in adjacencyOperators) { | |
_fn(key); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment