You are on page 1of 1

Namespace - a container for variables and for functions.

typically to keep
variables and functions with the same name separate.

JS doesn't have namespaces. Because of the nature of objects, we do not need


namespaces

code:
var greet = 'Hello!';
var greet = 'Hola!';
console.log(greet);
out:
Hola!

ie. these variables are created by different js files. this is a problem since one
will overwrite the other. namespaces can help.

in the case of namespaces, we will have a container for the english part and a
conatianer for the spanish part. we wil make namespaces by means of objects.

code:
var english = {};
var spanish = {};

english.greet = 'Hello!';
spanish.greet = 'Hola!';
console.log(english.greet);
//these 2 variables, even though they are both called greet, do not collide.

english.greet.greetings = 'Hello!'; //throws an error. undefined


what works is:
english.greetings = {}; // creates the obejct in memory, avoiding undefined.greet
english.greetings.greet = 'Hello!';

^this is where the object literalmsyntax can be more useful.

You might also like