If you're declaring namespaces in ASP.NET webforms, is it better to just use Type.registerNamespace or the usual way of var $Namespace = function() {
}
-
Is this really related to ASP.Net?AUSteve– AUSteve2011-03-25 01:42:11 +00:00Commented Mar 25, 2011 at 1:42
-
I dunno. That's what I'm working on right now. Is Type not restricted to ASP.NET?Jonn– Jonn2011-03-25 02:01:29 +00:00Commented Mar 25, 2011 at 2:01
-
Yeah sorry, it's an MS AJAX thing.AUSteve– AUSteve2011-03-25 02:57:58 +00:00Commented Mar 25, 2011 at 2:57
2 Answers
Here is a blog post describing Type.registerNamespace: http://dotnetslackers.com/Community/blogs/bmains/archive/2009/05/30/ajax-and-type-registernamespace-how-it-works.aspx
Basically the two methods are the same thing.
If you're using MS AJAX then go with Type.registerNamespace, otherwise stick to plain ol' JavaScript.
Comments
I prefer Type.registerNamespace(...) because it already handles creating sub-namespaces for you and won't overwrite existing namespaces.
Suppose you want to declare a namespace "A.B.C". Then you're talking about the difference between writing:
if(typeof A === "undefined") { A = function() { }; }
if(typeof A.B === "undefined") { A.B = function() { }; }
if(typeof A.B.C === "undefined") { A.B.C = function() { }; }
and:
Type.registerNamespace("A.B.C");
Obviously the latter is a time-saver and easier to read after the fact.