Skip to content

Instantly share code, notes, and snippets.

@jashkenas
Created December 10, 2010 22:18
Show Gist options
  • Save jashkenas/736920 to your computer and use it in GitHub Desktop.
Save jashkenas/736920 to your computer and use it in GitHub Desktop.
Snippet from Zepto.js, translated to CoffeeScript.
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]);
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
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