Advertisement
Guest User

ES5 polyfill (for IE/Edge)

a guest
Apr 19th, 2016
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. if (!Element.prototype.matches) Element.prototype.matches = Element.prototype.msMatchesSelector;
  2. if (!Element.prototype.closest) Element.prototype.closest = function(selector) {
  3.     var el = this;
  4.     while (el) {
  5.         if (el.matches(selector)) {
  6.             return el;
  7.         }
  8.         el = el.parentElement;
  9.     }
  10. };
  11.  
  12.  
  13. Array.prototype.last = function() {
  14.     return this[this.length - 1];
  15.     // undefined is returned for array[-1]
  16. }
  17.  
  18. Array.prototype.first = function() {
  19.     return this[0];
  20. }
  21.  
  22.  
  23. NodeList.prototype.forEach = NodeList.prototype.forEach || Array.prototype.forEach;
  24. NodeList.prototype.last = NodeList.prototype.last || Array.prototype.last;
  25. NodeList.prototype.first = NodeList.prototype.first || Array.prototype.first;
  26. NodeList.prototype.lastIndexOf = NodeList.prototype.lastIndexOf || Array.prototype.lastIndexOf;
  27. NodeList.prototype.indexOf = NodeList.prototype.indexOf || Array.prototype.indexOf;
  28. NodeList.prototype.some = NodeList.prototype.some || Array.prototype.some;
  29. NodeList.prototype.every = NodeList.prototype.every || Array.prototype.every;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement