Now I dive deeper into the world of .Net developers I notice more and more Microsoft ways of doing things unfamilair to me as a (once LiveScript) JavaScript developer.
I see PnP and other blogs using: Type.registerNamespace
The text below is a verbatim copy of http://dotnetslackers.com/Community/blogs/bmains/archive/2009/05/29/ajax-and-type-registernamespace-how-it-works.aspx
I'm triggered by the word supplement in the last sentence:
Microsoft does not attempt to replace any existing JavaScript conventions, but to supplement them.
My Questions are:
- is
Type.registerNamespace
something for .Net developers to ease them into JavaScript? - Are there advantages of using
Type.registerNamespace
?
(for me as a JavaScript dude in the Office365 world)
how Type.registerNamespace works
If you've developed in JavaScript without the ASP.NET AJAX client-side additions, you know that you typically can't create an object like the following:
function MyNS.MyClass() { }
This is not a supported feature because of the dot notation. This can work in JavaScript by setting up the namespace like the following:
var MyNS = MyNS || { };
And then adding the class to the instance. Using a variable is one way to create the namespace; using the window object is another. By using the following setup of a class:
if (!window["MyNS"]) window["MyNS"] = {};
window["MyNS"].MyClass = function() { }
This approach creates a new object MyClass within the MyNS namespace. MyNS is actually an object that has a MyClass property, which MyClass happens to be a function. This is perfectly valid. So an object can be created via:
var obj = new MyNS.MyClass();
The ASP.NET AJAX way of doing things is using the following approach:
Type.registerNamespace("MyNS");
MyNS.MyClass = function() { .. }
MyNS.MyClass.prototype = { .. }
MyNS.MyClass.registerClass("MyNS.MyClass");
Behind the scenes, ASP.NET AJAX works a very similar way. The Type.registerNamespace static method actually works by creating any global namespaces, and storing a reference with the window. What is a global namespace? For instance, if you had a class named Department.Hardware.Tools.Screwdriver, it would break down as follows:
Department <- global namespace Hardware <- local namespace Tools <- local namespace Screwdriver <- class
And thus, the registerNamespace method passes Department to the window object, creating a new class with the empty class "{}" notation. The Hardware namespace is a class that gets appended to Department, and so on. Notice that you assign the prototype the same way you would in both implementations. The prototype in ASP.NET AJAX and in JavaScript would be:
MyNS.MyClass.prototype = {
get_name: function () { return "my name"; }
}
This is because this is standard javascript; Microsoft does not attempt to replace any existing JavaScript conventions, but to supplement them.
My remarks
window["myNS"]
is the same aswindow.myNS
window["myNS"]=window["myNS"]||{}
has the same effect asvar myNS=myNS||{}
when executed on the global scope.
-- iJS