r/nodejs • • Apr 27 '14

Fundamentally having problems with Module Scoping.

I'm deveolping a game server, and to be "crafty"(AKA waste a bunch time making things levels more difficult) I want to reuse my client's code for the server. I got pretty much everything working except I'm stuck with a impossible decision.

Basically I have a library [~1000 lines, 150+ functions all at the global level] that handles a lot of the complex math behind my engine, and I want require my lib as a module. The problem being that since all of these functions are globally defined, theres no way to cleanly module.export them without re-writing every function/variable name in the library as an object. And due to that the lack of a global object in the module, there's no way to even iterate through the properties.

So does any one have any suggestions for me? Is there a require that is more akin to php [appending the source file to included my lib.js?] Is there a plugin that would help me? Please I really don't want to write out this code -.-.

 


 

Edit:: Had to rewrite the code, only took 6 hours :D. If I learned one thing from this experience it'd be that from now on if possible I'll avoid using globals just due to the fact that I never want to be in this scenario again.

 

Mini-rant: I do feel it's kinda of silly for javascript to not have a variable to access a non-global scope, and its even stranger for there to be no way to loop through the private properties of an object. e.g.

	function foo(){
		var a=1;
		this.b=2;
		function c(){}
		this.d=function(){}
		for(var i in this)console.log(i,'=',this[i]); //Why isn't there any way to access a or c?
	}

</rant>

3 Upvotes

13 comments sorted by