Javascript Namespace declarations in ASP.NET
If you're declaring namespaces in AS开发者_开发知识库P.NET webforms, is it better to just use Type.registerNamespace
or the usual way of var $Namespace = function() {
}
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.
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.
精彩评论