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


基础教程推荐
- fetch 是否支持原生多文件上传? 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01