从 Coffeescript 文件中导出一个类 [英] Export a class from a Coffeescript file

查看:34
本文介绍了从 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?

推荐答案

假设 exports 未定义且 this窗口.如果您遇到问题,请检查这些条件.

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 文件中导出一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆