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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Julian HansenCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
leakim971PluritechnicianCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.