我最近开始使用 coffeescript,并且很好奇将我使用 Coffeescript 创建的对象公开给其他 javascript 页面的“正确”方法是什么。由于 coffeescripts 包装功能,调用 window.coffeeObject = externalObject
是否可以接受。
例子
example.coffee
externalObject =
method1: -> 'Return value'
method2: -> 'Return method2'
window.myApi = externalObject
example.js -- 从 example.coffee 编译
(function() {
var externalObject;
externalObject = {
method1: function() {
return 'Return value';
},
method2: function() {
return 'Return method2';
}
};
window.myApi = externalObject;
}).call(this);
其他.js
alert(myApi.method1()) // Should return "Return value"
最佳答案
是的,没错。或者,您可以使用 define @myApi = { foo: -> }
因为 this
是文件根上下文中的 window
。
关于javascript - 使用 coffeescript 公开一个 javascript api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5995510/