Skip to content

Instantly share code, notes, and snippets.

@jgn
Created July 1, 2011 17:36
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 jgn/1059033 to your computer and use it in GitHub Desktop.
Save jgn/1059033 to your computer and use it in GitHub Desktop.
"Extending" a native JavaScript "class"
<script>
(function(){
// http://webreflection.blogspot.com/2008/10/subclassing-javascript-native-string.html
function MyString(__value__) {
this.length = (this.__value__ = __value__ || "").length;
};
with(MyString.prototype = new String)
toString = valueOf = function() { return this.__value__ };
MyString.prototype.reverse = function() {
var r = "";
for (i = this.__value__.length - 1; i >= 0; i--) {
r = r + this.__value__[i];
};
return r;
};
window.$ = function(s) {
return new MyString(s);
};
})();
</script>
<script>
alert($("!desreveR").reverse());
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment