使用 Coffeescript 公开 javascript api

发布于 2024-11-07 06:43:27 字数 791 浏览 0 评论 0原文

我最近开始使用 Coffeescript,并且很好奇将我使用 Coffeescript 创建的对象公开给其他 javascript 页面的“正确”方法是什么。由于咖啡脚本包装功能,调用 window.coffeeObject = externalObject 的行为是否可接受。

示例

example.coffee

externalObject = 
   method1: -> 'Return value'
   method2: -> 'Return method2'

window.myApi = externalObject

编译

(function() {
  var externalObject;
  externalObject = {
    method1: function() {
      return 'Return value';
    },
    method2: function() {
      return 'Return method2';
    }
  };
  window.myApi = externalObject;
}).call(this);

example.js -- 从 example.coffee other.js

alert(myApi.method1()) // Should return "Return value"

I recently started using coffeescript and was curious what is the "right" way to expose an object that I create with Coffeescript to other javascript pages. Because of coffeescripts wrapping functionality, is it acceptable behavior to call window.coffeeObject = externalObject.

Example

example.coffee

externalObject = 
   method1: -> 'Return value'
   method2: -> 'Return method2'

window.myApi = externalObject

example.js -- compiled from example.coffee

(function() {
  var externalObject;
  externalObject = {
    method1: function() {
      return 'Return value';
    },
    method2: function() {
      return 'Return method2';
    }
  };
  window.myApi = externalObject;
}).call(this);

other.js

alert(myApi.method1()) // Should return "Return value"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

仅一夜美梦 2024-11-14 06:43:27

是的,这是正确的。或者,您可以使用define @myApi = { foo: -> } 因为 this 是文件根上下文中的 window

Yep that's correct. Alternatively you can use define @myApi = { foo: -> } because this is window in the root context of the file.

萌辣 2024-11-14 06:43:27

您可以进一步简化语法,例如,如果您有 2 个内部函数

example.coffee

myPrivateFunction = ->
    "return 1"

myPrivateFunction2 = ->
    "return 2"

@myApi = {
    myFunction : myPrivateFunction,
    myFunction2 : myPrivateFunction2
}

example.js

this.myApi = {
  myFunction: myPrivateFunction,
  myFunction2: myPrivateFunction2
};

@ 将是 window 在文件的主要范围内。

然后通过 window.myApi.myFunction() 从其他地方调用

如果您想将外部函数名称映射到相同的内部名称,如果您不指定 key : value对,默认情况下它只会使用字符串值作为键。

example.coffee

@myApi = {
    myPrivateFunction,
    myPrivateFunction2
}

example.js

this.myApi = {
  myPrivateFunction: myPrivateFunction,
  myPrivateFunction2: myPrivateFunction2
};

You can simplify the syntax further, for example if you had 2 internal functions

example.coffee

myPrivateFunction = ->
    "return 1"

myPrivateFunction2 = ->
    "return 2"

@myApi = {
    myFunction : myPrivateFunction,
    myFunction2 : myPrivateFunction2
}

example.js

this.myApi = {
  myFunction: myPrivateFunction,
  myFunction2: myPrivateFunction2
};

The @ will be window within the main scope of the file.

Then call from elsewhere by window.myApi.myFunction()

If you wanted to map the external function names to the same internal names, if you don't specify key : value pairs, it will just use the string value as the key by default.

example.coffee

@myApi = {
    myPrivateFunction,
    myPrivateFunction2
}

example.js

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