问题描述
我最近开始使用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
example.js -- 编译自example.coffee
example.js -- compiled from example.coffee
other.js
推荐答案
是的,没错.或者,您可以使用 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!