JavaScript命名空间模式 [英] JavaScript Namespace Pattern
问题描述
- var nameSpace =( f 强>)();
另一个看起来像这样
- var nameSpace =( f ());
> f 是指完整的函数定义,如function(vars){....}。我知道 1 在将句柄返回给返回对象之前执行正文。但是, 2 与 1 不同?
他们都完成了完全相同的事情,但克罗克福德推荐了第二个。
我不知道你的意思是命名空间。他们都创建一个新的闭包,可以用于局部变量,所以我猜这是一个像命名空间。
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?
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.
这篇关于JavaScript命名空间模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!