Ultimate Library Stub for Javascript

Automated disclaimer: This post was written more than 15 years ago and I may not have looked at it since.

Older posts may not align with who I am today and how I would think or write, and may have been written in reaction to a cultural context that no longer applies. Some of my high school or college posts are just embarrassing. However, I have left them public because I believe in keeping old web pages aliveā€”and it's interesting to see how I've changed.

Combining the power of hot buzzwords such as "closures", "private and privileged scoping", "anonymous functions", and "object namespacing", I present a framework for future Javascript libraries.

The code itself

Without further ado, here is the stub:

if(typeof LibStub === 'undefined'){//begin double-include prevention

LibStub = new function()

	var version = '0.1';


	var youCantSeeMe = 3;


	function privateStuff()
		youCantSeeMe = 4;


	function init()


	//all throwaway public constants
	this.publicData = publicData;
	//all API methods
	this.init = init;
}//end double-include prevention

//JQuery to initialize library: $(document).ready(LibStub.init);

You can see it in action with a test harness.


The only innovation here is in exposing private methods so they become a privileged API. By keeping the API methods as private variables until the last minute, they are able to refer to each other without the parent object as a prefix or namespace, which can be unwieldy. Learn more about the other techniques:

No comments yet. Feed icon

Self-service commenting is not yet reimplemented after the Wordpress migration, sorry! For now, you can respond by email; please indicate whether you're OK with having your response posted publicly (and if so, under what name).