Yes, everything. Put it in a namespace. Everything. No exceptions and no excuses, unless yours is “I have just been thawed.” In this case, I want to be the first to warmly welcome you to the 21st century.
Here’s a simple and reasonably OK way to do it and be nice to your friends, other libraries and the world at large:
var Article = Article ? Article : new Object();
Article.title = "Report: School Shootings Help Prepare Students For Being Shot In Real World";
Article.save = function() {
alert("Saving " + this.title);
}
You could save a few keystrokes, though. Just use the object literal notation directly:
var Article = Article ? Article : {
title: "Report: School Shootings Help Prepare Students For Being Shot In Real World",
save: function() {
alert("Saving " + this.title)
}
}
These two last examples are great if you’re not that concerned about exposing the ‘title’ attribute to the rest of the world. If there is a chance that problems could arise if some other piece of code changed it directly, there is a solution:
var Article = Article ? Article : function() {
var private = {
title: "Report: School Shootings Help Prepare Students For Being Shot In Real World"
};
var public = {
getTitle: function() {
return private.title;
},
save: function() {
alert("Saving " + this.getTitle());
}
}
return public;
}();
I find this a bit hard to get used to, after so many years of developing in languages that explicitly allow me to set access control. It makes sense, though: by creating an anonymous function that returns the object I want to define, and then immediately calling it (note the ‘()’ at the last line), I can hide whatever I don’t want other parts of the code to see – it’s all tucked away in the local context of that anonymous function.

Guilherme Chapiewski | 15-Sep-07 at 3:58 am | Permalink
Cool but… what a weird syntax!!
Simon Brunning | 15-Sep-07 at 6:04 pm | Permalink
Going to implement this throughout R2?
Carlos Villela | 15-Sep-07 at 9:09 pm | Permalink
Guess I could always try!
chris clarke » Private, Public and Static in JavaScript | 22-Sep-07 at 4:29 pm | Permalink
[...] you can use the approach Carlos Villela suggests and define your public and private methods in different objects inside the constructor and return [...]
cmondkey | 25-Sep-07 at 11:24 am | Permalink
That’s cool Carlos. There’s a Javascript library out there that does the same thing, but provides simpler end-user syntax. It’s called Ajile and let’s you define and import namespaces with syntax like:
Namespace (“Article”); or Namespace(“org.lixo.www”);
and Import (“Article.*”);
You should take a look at Ajile, really makes creating and using Javascript namespaces easy…
Carlos Villela | 01-Oct-07 at 1:04 am | Permalink
Ajile looks really interesting indeed. Thanks!
RJ Budzynski | 03-Oct-07 at 7:29 pm | Permalink
You could have saved a few keystrokes by writing
var Article = Article || function () { …
links for 2007-10-04 « Simply… A User | 04-Oct-07 at 12:40 am | Permalink
[...] lixo.org :: JavaScript: Put everything in a namespace (tags: javascript namespace webdev code **) [...]
Matt Underwood | 04-Oct-07 at 10:19 am | Permalink
I’ve never seen this notation before,
var Article = Article ? Article : function() {…
or
var Article = Article || function () {…
Is this to prevent Article from being redefined – for example if the article.js file was included more than once?
Jason Grunstra | 04-Oct-07 at 11:02 pm | Permalink
Back to back articles on closures! No sooner had I finished reading this post than I stumbled upon this one by Dustin Diaz:
http://www.dustindiaz.com/roll-out-your-own-interface/
In the comments over there someone link s to this great article over on the YUI site that explains in great detail the Module Pattern: http://yuiblog.com/blog/2007/06/12/module-pattern/
It’s all starting to sink in.
Menno van Slooten | 05-Oct-07 at 1:33 pm | Permalink
Interesting notation. Could you please explain to me how this is better than:
function Article() {
// private :
var title = ‘I am a private title’;
// public
this.getTitle = function() {
return title;
}
this.save = function() {
alert(‘Saving ‘ + this.getTitle());
}
}
All in a days work… | 05-Oct-07 at 2:19 pm | Permalink
[...] JavaScript: Put everything in a namespace By creating an anonymous function that returns the object I want to define, and then immediately calling it, I can hide whatever I don’t want other parts of the code to see – it’s all tucked away in the local context of that anonymous function. [...]
» Javascript Namespaces Flusensieb | 27-Jan-08 at 10:06 am | Permalink
[...] More Infos: dustindiaz.com, lixo.org [...]
Dr. Prolix » Blog Archive » Enliven your namespaces | 01-May-08 at 5:09 pm | Permalink
[...] practice to use namespaces when writing your JavaScript. The reasons why have been stated time and again by much smarter and more eloquent people than myself, so suffice it to say, you should do [...]
efeefe | 02-Jun-08 at 3:27 pm | Permalink
Sistemas de aprendizado…
…
ls.n | 25-Aug-08 at 6:20 am | Permalink
Enliven your namespaces…
It’s a common good practice to use namespaces when writing your JavaScript. The reasons why have been stated time and again by much smarter and more eloquent people than myself, so suffice it to say, you should do it. The……
dennis | 31-Oct-08 at 3:27 pm | Permalink
Thank you very much
strony internetowe k | 29-Nov-08 at 4:30 pm | Permalink
Thanks for this article, it’s great. So great that we’ve made it ‘sticky’ on The Webmaster Forums. Now we don’t have to repeat ourselves, just send people to this article!
Matt DeClaire | 28-Apr-09 at 7:03 pm | Permalink
I wrote an article regarding the importance of namespacing in JavaScript.
JavaScript Standards - Not Just a Hat Rack | 11-Aug-09 at 7:24 pm | Permalink
[...] http://www.lixo.org/archives/2007/09/14/javascript-put-everything-in-a-namespace/ [...]