0

I've recently come across JavaScript Namespaces and how you can use them to create namespaces like other popular OOP languages. I have confusion on how they are declared. For example,

var myNamespace = myNamespace || {};

creates a namespace called myNamespace if it hasn't already been created. If it was created, just pass it into var myNamespace.

I have trouble understanding what myNamespace || {} actually does.

Wouldn't myNamespace be undefined at first? How would you compare that in a Boolean expression.

Also, how does the object literal {} work? Does it create an empty object and assign that to myNamespace to work as a namespace?

I've tried looking for the answer on SO, but there's too much saturation about the practices on how to declare different types of namespaces.

Jimenemex
  • 3,104
  • 3
  • 24
  • 56

3 Answers3

1

var myNamespace = myNamespace || {};

Does exactly what you said it does.

|| is an or operator as seen here

So what this is doing is, saying var myNamespace = myNamespace however, if myNamespace is undefined, then it will just go to the right hand side of the operator and just create an empty object like this (you were right about this):

var myNamespace = {}

Let me know if I seem confused, I think I addressed your points.

jdmdevdotnet
  • 1
  • 2
  • 19
  • 50
1

Although || was originally intended to work with booleans, it is implemented in a very handy way that allows it to work to set default values:

function or(a, b) {
  if (a) return a
  else return b
}

You can test this in your head and see that it works with booleans: if a is true, the result is true so the result is the same as a. If a is false then the result depends on b.

Variable declarations with var in JavaScript work a bit strangely. You are allowed to redeclare a variable with the same identifier in the same scope. So the way the code works is:

  • If myNamespace is already declared, you redeclare the variable without assigning to it yet. You get the value from the old declaration of myNamespace. This value is assumed to be truthy, so the || will return it.
  • If myNamespace is not yet declared, you declare it and then myNamespace will be undefined, a falsy value. So the || will return the second value, the {}, and that is the value that will get assigned to myNamespace.

It may make sense to read it in two separate steps:

//Declare the variable if it doesn't exist,
//without overwriting an existing value
var myNamespace
//Take the value of myNamespace (undefined if it didn't exist yet)
//and replace it with {} if it is falsy
myNamespace = myNamespace || {}
csander
  • 1,385
  • 10
  • 11
1

Take a look at the definition of || from here:

expr1 || expr2

Returns expr1 if it can be converted to true; otherwise, returns expr2.

Now try

console.log(undefined == true);

This will print false, which means that undefined cannot be converted to true (we are using the == operator to let JavaScript use type coercion). As a result, if myNamespace is undefined, an empty object literal will by assigned to it (which is equivalent to expr2 from the quote).

Kapol
  • 6,383
  • 3
  • 21
  • 46