Learn MORE JavaScript on the ServiceNow Platform: Lesson 7 - The practice of Namespacing
okay, welcome back. Today's episode of Learn More JavaScript on the ServiceNow
platform is all about tidiness and order. We're going to talk about the practice
of namespacing. In the world of JavaScript
namespacing is a technique to avoid name collisions
and keep our global scope clear. Let's
go ahead and start organizing your code. Okay,
So name spacing is essentially creating a unique object in the global scope
that becomes your application’s... mini... like a mini universe. All your functions and variables
live inside this namespace and it's safe from outside interference. It's safe from outside conflicts
and this practice is not just for cleanliness, it's for maintaining
sanity in larger code bases. So let me go ahead
and show you an example. Okay. I just copy
and pasted an example of a namespace set up inside of the script editor. As you can see,
instead of putting functions just outside on the global scope or putting functions
right before I call them, I'm loading my functions
and I'm loading my variable names. I'm loading my strings instead I'm loading my functions inside of this thing
called MyServiceNowApp. And I might do other things inside of this code space, this name space and stuff like my name, and I'm going to put my name in here
instead. Instead of putting it in the global scope, instead of putting it somewhere like here. And the reason why we're doing this is because of cleanliness,
and it keeps things a lot more organized. and if I want to do anything
within my the rest of my script, I know to call it via this overarching MyServiceNowApp object
and this helps me keep things organized because if I have another object
that has a different purpose, then it helps me
keep it organized there. And now I know where things are. It makes me avoid collisions
with other names. If I am using a lot of variables
throughout the script, it just keeps everything a little bit
more organized. Namespacing is actually used a lot
within ServiceNow out of the box. You can actually go to the script
includes table and go venture around any of the bigger script
includes that we have in there. Click around
and you'll see that a lot of them will have namespaces at the beginning
of their script includes or on the the overarching script
include to help people know where is this function coming from,
where is this method coming from? It helps
keep things a little bit more organized. And so that was a really short lesson. But namespacing is like
giving your code its own room where it can't be messed up by other scripts
running on the same platform. Especially as your applications grow, this technique
becomes increasingly important to just prevent conflicts
and to maintain that code cleanliness. Throughout the series,
we didn't talk so much about like that standards of cleanliness and stuff
like that, but this kind of gets into it. And so it is it's helpful. It's important to know,
and it's a concept that we wanted to make sure you all knew about.
https://www.youtube.com/watch?v=diS9Mqq5EeI