Skip to content

Instantly share code, notes, and snippets.

@radovansurlak
Last active April 2, 2017 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save radovansurlak/98f6c631488a278eb32bb15c9b515b67 to your computer and use it in GitHub Desktop.
Save radovansurlak/98f6c631488a278eb32bb15c9b515b67 to your computer and use it in GitHub Desktop.
Extending String and Array Prototypes - Render To Document - ES6
String.prototype.renderize = function (type, className, idName) {
return typeof type != 'undefined' ?
`<${type}${ typeof className != 'undefined' ? ' class=\'' + className + '\'' : '' }${typeof idName != 'undefined' ? ' id=\'' + idName + '\'' : ''}> ${this} </${type}>`
: new Error ('Unspecified HTML DOM element type (1st argument)')
}
String.prototype.render = function (type, className, idName) {
document.write(this.renderize(type,className,idName))
}
Array.prototype.render = function (type, className, idName) {
this.map( x => {
document.write(x.renderize(type, className, idName))
})
}
let text = ["This is the first sentence." ,"And this is another one."]
text.render('h2')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment