Skip to content

Instantly share code, notes, and snippets.

@oomlaut
Created July 12, 2013 17:47
Show Gist options
  • Save oomlaut/5986328 to your computer and use it in GitHub Desktop.
Save oomlaut/5986328 to your computer and use it in GitHub Desktop.
Extend the String superclass to enable a more OO-elegant way of joining strings.
(function(){
String.prototype.concat = function(argument){
if ( arguments.length > 0 ){
if ( typeof argument === "string" ) {
return this + argument;
} else {
var str = "";
for ( var i in argument ){
str += argument[i];
}
return this + str;
}
}
return this;
};
})();
console.log("foo".concat("bar"));
console.log("Hello".concat([" World", "!"]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment