Namespace Not Defined

I was looking at the following article:

https://blogs.perficient.com/perficientdigital/2017/11/21/detecting-the-use-of-a-touch-screen/

At the top of the code, the "WEB" namespace is referred to.  How do I tell what "WEB" namespace is referencing?  If I copy the code directly, I get Uncaught ReferenceError: WEB is not defined on the first line. I thought maybe it was a system namespace?

// Make sure that the `WEB` namespace object exists and the `utils` container exists within it
WEB = WEB || {};
WEB.utils = WEB.utils || {};
...

Open in new window

894359Asked:
Who is Participating?
 
Julian HansenConnect With a Mentor Commented:
then why do I get Uncaught ReferenceError: WEB is not defined on the first line?
because you are not declaring the WEB variable anywhere

If you do this it works - not the 'var' before the variable.
// Make sure that the `WEB` namespace object exists and the `utils` container exists within it
var WEB = WEB || {};
WEB.utils = WEB.utils || {};
...

Open in new window


What is happening is that on the left you have WEB which is ok but you are setting that equal to variable WEB (on the right) which does not exist yet so you are getting the error.

By putting 'var' in front WEB is defined and then being reassigned with the right hand side expression.

Consider what happens with these two examples
<script>
var WEB = {a:1};
var WEB = WEB || {};
WEB.utils = WEB.utils || {};
console.log(WEB);
</script>

Open in new window

Output
Object { a: 1, utils: {} }

Open in new window

And this
<script>
//var WEB = {a:1};
var WEB = WEB || {};
WEB.utils = WEB.utils || {};
console.log(WEB);
</script>

Open in new window

Output
Object { utils: {} }

Open in new window

In the first the WEB variable is overwritten with the previously defined WEB - in this case the second var is unnecessary BUT we have to cater for the second example where there is no WEB previously defined.
0
 
leakim971PluritechnicianCommented:
replace by :
WEB = WEB?WEB:{};
WEB.utils = WEB.utils?WEB.utils:{};

The goal is to be sure you're not overwritting a current WEB object, so you create an empty namespace ( using {} ) OR reusing the existing one and add the utils "container"
0
 
894359Author Commented:
Hi Leakim971 then why do I get Uncaught ReferenceError: WEB is not defined on the first line?
0
 
leakim971Connect With a Mentor PluritechnicianCommented:
why do I get Uncaught ReferenceError: WEB is not defined on the first line?

As I said in my first answer, the guy (not me) don't want to overwritte an  existing WEB object (using var WEB, you replace, in youir scope, an existing WEB object in your scope or global scope)

You may ask : why did he that ?
Because you assume this guy is perfect, he can't miss some points.
Nobody is perfect, me first
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.