Last active
April 2, 2017 14:08
-
-
Save radovansurlak/98f6c631488a278eb32bb15c9b515b67 to your computer and use it in GitHub Desktop.
Extending String and Array Prototypes - Render To Document - ES6
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
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