Skip to content

Instantly share code, notes, and snippets.

@hassox
Created January 31, 2010 12:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hassox/291054 to your computer and use it in GitHub Desktop.
Save hassox/291054 to your computer and use it in GitHub Desktop.
Object.prototype.autoload = function(type, filePath, includeAsType){
var obj = undefined;
if(includeAsType === undefined) includeAsType = true;
this.__defineGetter__(type, function(){
if(!obj){
obj = require(filePath);
if(includeAsType) obj = obj[type];
}
return obj;
});
}
var sys = require('sys');
var paths = require('path');
require('./autoload');
var foo = {}
foo.autoload("MyObj", paths.join(__dirname, "myObj"));
sys.puts(foo.MyObj);
exports.MyObj = {
hi : "there"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment