Skip to content

Instantly share code, notes, and snippets.

@frontdevops
Last active October 2, 2019 13:30
Show Gist options
  • Save frontdevops/68315b9075d9ee84ce6b2b34e03e0155 to your computer and use it in GitHub Desktop.
Save frontdevops/68315b9075d9ee84ce6b2b34e03e0155 to your computer and use it in GitHub Desktop.
Short jQuery implementation on ES7+
const $ = ::document.querySelectorAll;
{
let p = Element.prototype;
p.on = Element.prototype.addEventListener;
p.find = function(s){ return this.querySelector(s) };
p.html = function(s){
if(!s) return this.innerHTML;
this.innerHTML = s;
return this;
};
p = NodeList.prototype;
p.on = function(e,f){
this.forEach($=>$.on(e,f));
return this;
};
p.html = function(s) {
let a = Array.prototype;
return a.reduce.call(
a.map.call(this, $=>$.html(s)),
(a, b) => a + b
);
return this;
};
}
// Usage
let alrt = function(){ alert(this.innerText) };
$('#btn0').on('click', alrt);
$('section > button').on('click', alrt);
$('section > div').html('<span>Set html text</span>');
$('pre')[0].html( $('div').html() );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment