8

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 as window.myNS
  • window["myNS"]=window["myNS"]||{} has the same effect as var myNS=myNS||{}
    when executed on the global scope.

-- iJS

  • It is just a personal theory, but it is the back end guys trying to apply back end principles to front end technology, which is why you see things like TypeScript. – Eric Alexander Jan 14 '16 at 14:12
  • For now I agree the first part. But (Microsoft) TypeScript has another background and a promising future as a Javascript superset language now some Google teams have chosen TypeScript over other (Google) developments. The browser wars are over, the (javascript) language wars are almost over.. I guess the next (front-end) battle will be (Google)Material versus (Microsoft)Fabric versus whatever Facebook calls it what they do which designers not even call Design – Danny '365CSI' Engelman Jan 14 '16 at 14:33
4

There aren't any technical advantages to using this method. It simply allowed developers to use the same conventions they used on the client (JavaScript) as they used on the server (ASP.NET SharePoint solutions).

The JavaScript Object Model code is transpiled from the C# Client Object Model -- lots of things in the Microsoft Ajax libraries exist to make this transpiling work properly and to make working with JavaScript similar to working with the C# libraries, which made client side coding easier for ASP.NET developers to get started with.

This would have been advantageous at the time the JSOM was developed (circa 2009) since most SharePoint developers would have been experienced ASP.NET developers and not necessarily experienced JavaScript developers.

  • 1
    tnx John, I opened Visual Studio once a long time ago when a SharePoint Consultant had to do .Net. I have done classic ASP but SharePoint backend just wasn't my thing. Coming from a web background I feel like an Englishman in New York sometimes. – Danny '365CSI' Engelman Jan 16 '16 at 11:38
1

Update

Type.registerNamespace makes MDS - Minimal Dowload Strategy work

CSR + MDS, where are my globals going?? (and I want them back!)


  • Essential? Not really. It's useful for keeping global variables around (which are still problematic for all the usual reasons) but there are at least 3 ways of working with MDS that don't involve using Type.registerNamespace: sharepoint.stackexchange.com/questions/165150/… – John-M Mar 20 '16 at 12:44
  • This thing is giving me a head-egg (I'm building a tool to drop easter-eggs behind WebParts with JSlinks) DOM manipulation outside the CSR scope is my currect challenge (on MDS enabled sites). If add DOM elements on page A, I remove them when browsing to page B.. on return on page A, no script fires (I am working my way into the asyncDeltaManager now.... getting there) – Danny '365CSI' Engelman Mar 20 '16 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.