问题
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:
- var nameSpace = (f)();
the other one looks like this
- 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?
回答1:
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.
来源:https://stackoverflow.com/questions/18690508/javascript-namespace-pattern