Created
January 22, 2020 16:47
-
-
Save ivandoroshenko/0a7f91cfbdd9ae28eac04a5c20218def to your computer and use it in GitHub Desktop.
js prepend IE polyfill
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
(function (arr) { | |
arr.forEach(function (item) { | |
if (item.hasOwnProperty('prepend')) { | |
return; | |
} | |
Object.defineProperty(item, 'prepend', { | |
configurable: true, | |
enumerable: true, | |
writable: true, | |
value: function prepend() { | |
var argArr = Array.prototype.slice.call(arguments), | |
docFrag = document.createDocumentFragment(); | |
argArr.forEach(function (argItem) { | |
var isNode = argItem instanceof Node; | |
docFrag.appendChild(isNode ? argItem : document.createTextNode(String(argItem))); | |
}); | |
this.insertBefore(docFrag, this.firstChild); | |
} | |
}); | |
}); | |
})([Element.prototype, Document.prototype, DocumentFragment.prototype]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment