Created
January 15, 2011 06:09
-
-
Save ryanflorence/780728 to your computer and use it in GitHub Desktop.
Experimental extending of native objects without touching prototypes
This file contains 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
;(function(){ | |
function typeOf(item){ | |
if (item._family) return item._family(); | |
return typeof item; | |
} | |
Array.prototype._family = function(){ return 'array'; }; | |
var _ = this._ = function(obj){ | |
if (obj._extended) return obj; | |
var wrapper = { | |
_obj: obj, | |
_type: typeOf(obj), | |
_extended: true, | |
get: function(){ return wrapper._obj } | |
}; | |
for (i in exts[wrapper._type]){ | |
if (exts[wrapper._type].hasOwnProperty(i)){ | |
wrapper[i] = function(){ | |
return exts[wrapper._type][i].call(wrapper._obj, [].slice.call(arguments, 0)); | |
} | |
} | |
} | |
return wrapper; | |
}; | |
var exts = { array: {}, number: {}, string: {} }; | |
_.define = function(type, name, fn){ | |
exts[type][name] = fn; | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage: http://jsfiddle.net/rpflorence/DZEPZ/