JavaScript Namespace Pattern

假如想象 提交于 2020-01-16 00:39:10

问题


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?


回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!