Extend the String Method Example


/ Published in: JavaScript
Save to your folder(s)

Simple extension of the string method using string.prototype.


Copy this code and paste it in your HTML
  1. function fmtHeading(level) {
  2. stringtext = this.toString();
  3. starttag = "<h" + level + ">";
  4. endtag = "</h" + level + ">";
  5. return starttag + stringtext + endtag;
  6. }
  7.  
  8. String.prototype.heading = fmtHeading;
  9. document.write("This is a heading!".heading(2));

Report this snippet


Comments


You need to login to view comments.