public
Last active

CommonJS: Extending native prototypes from a module

  • Download Gist
commonjs_app.js
JavaScript
1 2 3
require('footools').setup(Array, Boolean, Function, Number, RegExp, String);
 
"bar".foo(); // no Fail
commonjs_mod.js
JavaScript
1 2 3
require('footools').setup(Array, Boolean, Function, Number, RegExp, String);
 
"bar".foo(); // no Fail
footools.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
// Framework codez
 
function FooTools(){
this.String.prototype.foo = function(){ return "foo'd"; };
};
 
 
// Exporting
 
if (typeof exports == 'undefined') var exports = {};
 
exports.setup = function(Array, Boolean, Function, Number, RegExp, String){
var context = {
Array : Array,
Boolean : Boolean,
Function : Function,
Number : Number,
RegExp : RegExp,
String : String
};
FooTools.apply(context);
};
web_app.html
HTML
1 2 3
<script src="mod.js"></script>
<script>FooTools.apply(window);</script>
<script>"bar".foo(); /*no Fail*/</script>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.