Expose a javascript api with coffeescript(使用 coffeescript 公开一个 javascript api)
问题描述
我最近开始使用coffeescript,并且很好奇将我用Coffeescript 创建的对象公开给其他javascript 页面的正确"方式是什么.由于咖啡脚本包装功能,调用 window.coffeeObject = externalObject
是否可以接受.
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.coffee
externalObject =
method1: -> 'Return value'
method2: -> 'Return method2'
window.myApi = externalObject
example.js -- 编译自example.coffee
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"
推荐答案
是的,没错.或者,您可以使用 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.
这篇关于使用 coffeescript 公开一个 javascript api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 coffeescript 公开一个 javascript api


- 是否可以将标志传递给 Gulp 以使其以不同的方式 2022-01-01
- 如何调试 CSS/Javascript 悬停问题 2022-01-01
- 为什么悬停在委托事件处理程序中不起作用? 2022-01-01
- 为什么我的页面无法在 Github 上加载? 2022-01-01
- 在不使用循环的情况下查找数字数组中的一项 2022-01-01
- 使用 iframe URL 的 jQuery UI 对话框 2022-01-01
- 如何向 ipc 渲染器发送添加回调 2022-01-01
- 我不能使用 json 使用 react 向我的 web api 发出 Post 请求 2022-01-01
- 从原点悬停时触发 translateY() 2022-01-01
- 如何显示带有换行符的文本标签? 2022-01-01