本文介绍了从Coffeescript文件导出类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有一个Coffeescript类定义在一个单独的文件,我从我的主脚本调用。我可以使文件内的函数全局可见,但不是类。
If I have a Coffeescript class defined in a separate file which I'm calling from my main script. I can make the functions within the file globally visible, but not the class.
包含的文件是:
root = exports ? this
root.add = (a, b) ->
return a + b
class root.userModel
username: 'Aaaa'
name: 'Bbbb'
我可以从我的主代码访问该函数。如何创建类?
I can access the function from my main code. How can I create the class?
推荐答案
您的代码确实会使 userModel
一个全局变量,假设 exports
未定义,此
为窗口
。如果您遇到问题,请检查这些条件。
Your code will indeed make userModel
a global, assuming that exports
is undefined and this
is window
. If you're having problems, check those conditions.
这篇关于从Coffeescript文件导出类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!