Coffeescript 中带有隐藏变量的模块模式
深入研究 Coffeescript 我正在尝试将我的 Javascript 文件移植到 Coffeescript。
关于这一点,我有一个与 Doulgas Crockford 的模块模式相关的问题(闭包绑定以保持变量“私有”)
因此我的问题是:以下 JS 的等效 Coffeescript 是什么样子:
var test = function () { var hidden = 'open'; return { open: hidden }; }();
分别,是否有不同的/ Coffeescript 中这种模式的更好方法?
Digging into Coffeescript I am trying to port my Javascript files to Coffeescript.
Concerning this, I have a question related to the module pattern of Doulgas Crockford (closure binding in order to keep variables "private")
Therefore my question is: What would the aquivalent Coffeescript for the following JS look like:
var test = function () { var hidden = 'open'; return { open: hidden }; }();
Respectively, is there a different / better aproach to this pattern in Coffeescript?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为最好的方法是在
do
关键字的帮助下将您的示例逐字翻译为 CoffeeScript(该关键字主要用于捕获循环中的值 - 请参阅我的 PragPub 文章):此编译结果
与您的代码(格式除外)相同。 (CoffeeScript 编译器自动将所有
var
声明放在其作用域的顶部,这样可以通过查看 JavaScript 输出轻松确定变量的作用域。)I think the best approach is to literally translate your example into CoffeeScript, with the help of the
do
keyword (which exists mainly to capture values in loops—see my PragPub article):This compiles to
which is identical to your code other than formatting. (The CoffeeScript compiler automatically puts all
var
declarations at the top of their scope, which makes it easy to determine how a variable is scoped by looking at the JavaScript output.)我在 CoffeeScript Wiki 中添加了一个关于如何处理命名空间的部分。它非常优雅(我认为)
https://github.com/ jashkenas/coffee-script/wiki/Easy-modules-with-coffeescript
Coffeescript 没有将所有源代码文件封装在匿名函数中的本机模块系统。然而,通过一些简单的技巧,您就可以拥有让 Ruby 羡慕的模块。
我定义了我的模块,如下所示。
如果您愿意,您可以将模块助手的实现
放入另一个源文件中。然后,您可以通过命名空间模块访问您的类
,以解决您的具体问题,我认为下面的茉莉花规范使用我的来回答它
模块系统
I added a section to the coffeescript wiki on how I handle namespacing. It's pretty elegent ( I think )
https://github.com/jashkenas/coffee-script/wiki/Easy-modules-with-coffeescript
Coffeescript does not have a native module system above that of enclosing all source code files in an anonymous function. However with a bit of simple trickery you can have modules that are the envy of Ruby.
I define my modules like below
The implementation of the module helper is
which you can put in another source file if you like. You can then access your classes by namespaced modules
wrt to your specific question I think the below jasmine spec answer it using my
module system
CoffeeScript(或者更确切地说,
coffee
脚本)会自动将您的代码包装在匿名函数中,除非您告诉它不要这样做。如果您需要从该匿名闭包中发布对象,您可以将它们显式分配给根对象;请参阅 Underscore.coffee 的开头以获取一些指示。
http://jashkenas.github.com/coffee-script/documentation/docs/underscore.html< /a>
CoffeeScript (or rather, the
coffee
script) automatically wraps your code within an anonymous function unless you tell it not to.If you need to publish objects from within that anonymous closure, you can explicitly assign them to the root object; see the start of Underscore.coffee for some pointers.
http://jashkenas.github.com/coffee-script/documentation/docs/underscore.html
如果您可以在单个类中编写模块,那么使用
-b
选项编译咖啡脚本自然会创建您正在寻找的模块模式。这:
编译为:
这非常接近您正在寻找的内容。
If you can write your module in a single class, then compiling the coffeescript with the
-b
option will naturally create the module pattern you're looking for.This:
compiles to this:
which is very nearly what you were looking for.