JavaScript命名空间模式 [英] JavaScript Namespace Pattern

查看:102
本文介绍了JavaScript命名空间模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了这个JavaScript错综复杂的问题,并在努力寻找差异。它关于JavaScript命名空间。我的问题很简单,命名空间定义的一种形式如下所示:


  1. var nameSpace =( f )();

另一个看起来像这样


  1. 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:

  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?

解决方案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆