Different require proof of concept, abusing eval for the sake of clearer definitions




// Sample module libraries (would probably be in their own files)
someModules = { 
	testModule: {test: function(){console.log("test from someModule")}},
	anotherModule: { doStuff: function(){console.log("Doin stuffs!");}}
};

sampleRequire = function() {
	
	// Load the modules
	for (var i=arguments.length-2; i>=0; --i){

		// Create a local variable reference to the module
		eval ('var '+arguments[i]+' = someModules.'+arguments[i].toString());
	}

	// Redefine the programmer's function so that it has my local vars in its scope
	eval("var fn = "+arguments[arguments.length-1]);

	return fn;
}

// Main code...
sampleRequire( 'testModule', 'anotherModule',
 	function(){ 
		testModule.test();
		anotherModule.doStuff();
	}
)();
Advertisements

About andyortlieb

I've been a tinkerer and a lazy ideologist since I gained my first personal computer in 1989. I have an aversion to formal education and I prefer to learn things by experimentation. That is not a brag, it has not been a particularly helpful set of traits. My primary goals are to increase my safety, pleasure and usefulness as I work to build and modify technologies that will bring those same attributes to the masses irrespective of government. This is the documentation of my attempt at that journey.
This entry was posted in Uncategorized and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s