Choksi
Choksi

Reputation: 43

JavaScript Namespace Pattern

I came across this JavaScript intricacy and was struggling to find the difference. Its about JavaScript namespaces. My question is simple, one form of namespace definition looks like this:

  1. var nameSpace = (f)();

the other one looks like this

  1. var nameSpace = (f ());

here f refers to the full function definition like function(vars) { ....}. I know that 1 executes the body before returning the handle to the return object. But how does 2 differ from 1?

Upvotes: 1

Views: 1360

Answers (1)

Joseph Silber
Joseph Silber

Reputation: 220036

They both accomplish the exact same thing, but Crockford recommends the second one.

I'm not sure what you mean by "namespace" though. They both create a new closure, which you can use for local variables, so I guess it's kind of like a namespace.

Upvotes: 1

Related Questions