Coffeescript 中带有隐藏变量的模块模式

发布于 2024-11-09 08:47:59 字数 320 浏览 0 评论 0原文

深入研究 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜访吸血鬼 2024-11-16 08:47:59

我认为最好的方法是在 do 关键字的帮助下将您的示例逐字翻译为 CoffeeScript(该关键字主要用于捕获循环中的值 - 请参阅我的 PragPub 文章):

test = do ->
  hidden = 'open'
  open: hidden

此编译结果

var test;
test = (function() {
  var hidden;
  hidden = 'open';
  return {
    open: hidden
  };
})();

与您的代码(格式除外)相同。 (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):

test = do ->
  hidden = 'open'
  open: hidden

This compiles to

var test;
test = (function() {
  var hidden;
  hidden = 'open';
  return {
    open: hidden
  };
})();

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.)

静谧幽蓝 2024-11-16 08:47:59

我在 CoffeeScript Wiki 中添加了一个关于如何处理命名空间的部分。它非常优雅(我认为)

https://github.com/ jashkenas/coffee-script/wiki/Easy-modules-with-coffeescript

Coffeescript 没有将所有源代码文件封装在匿名函数中的本机模块系统。然而,通过一些简单的技巧,您就可以拥有让 Ruby 羡慕的模块。
我定义了我的模块,如下所示。

@module "foo", ->
    @module "bar", ->
        class @Amazing
            toString: "ain't it"

如果您愿意,您可以将模块助手的实现

window.module = (name, fn)->
  if not @[name]?
    this[name] = {}
  if not @[name].module?
    @[name].module = window.module
  fn.apply(this[name], [])

放入另一个源文件中。然后,您可以通过命名空间模块访问您的类

x = new foo.bar.Amazing

,以解决您的具体问题,我认为下面的茉莉花规范使用我的来回答它
模块系统

@module "test", ->
  hidden = 10
  @open  = hidden

describe "test", ->
  it "has no hidden", ->
    expect(test.hidden?).toEqual false

  it "has  open", ->
    expect(test.open?).toEqual true

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

@module "foo", ->
    @module "bar", ->
        class @Amazing
            toString: "ain't it"

The implementation of the module helper is

window.module = (name, fn)->
  if not @[name]?
    this[name] = {}
  if not @[name].module?
    @[name].module = window.module
  fn.apply(this[name], [])

which you can put in another source file if you like. You can then access your classes by namespaced modules

x = new foo.bar.Amazing

wrt to your specific question I think the below jasmine spec answer it using my
module system

@module "test", ->
  hidden = 10
  @open  = hidden

describe "test", ->
  it "has no hidden", ->
    expect(test.hidden?).toEqual false

  it "has  open", ->
    expect(test.open?).toEqual true
拥有 2024-11-16 08:47:59

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

段念尘 2024-11-16 08:47:59

如果您可以在单个类中编写模块,那么使用 -b 选项编译咖啡脚本自然会创建您正在寻找的模块模式。

这:

class test
    hidden = 'open'
    open: hidden

编译为:

var test;
test = (function() {
    var hidden;
    hidden = 'open';

    test.prototype.open = hidden;

    return test;
})();

这非常接近您正在寻找的内容。

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:

class test
    hidden = 'open'
    open: hidden

compiles to this:

var test;
test = (function() {
    var hidden;
    hidden = 'open';

    test.prototype.open = hidden;

    return test;
})();

which is very nearly what you were looking for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文