I need to use a JavaScript library in my CoffeeScript application. Since I am not familiar with both languages I try something simple. My coffeescript file:
我需要在CoffeeScript应用程序中使用JavaScript库。由于我不熟悉这两种语言,我尝试了一些简单的方法。我的coffeescript档案:
empty = require('models/empty')
class Contact extends Spine.Model
@configure 'Contact', 'name', 'email'
@extend Spine.Model.Local
create: ->
empty.one()
super
module.exports = Contact
And my Javascript file named empty.js :
我的Javascript文件名为empty.js:
console.log('what')
function one () {
console.log('one')
};
The coffeescript file works normally, although I cant get empty.one() to work. 'what' is printed on console which means that the JS file is loaded. Although I get the following error when one() is called:
coffeescript文件正常工作,虽然我不能使empty.one()工作。 'what'打印在控制台上,表示已加载JS文件。虽然调用one()时出现以下错误:
Uncaught TypeError: Object # has no method 'one'
未捕获的TypeError:对象#没有方法'one'
I have tried many different ways of defining the function, as variable, and using different syntaxes I found on tutorial, although none of this seems to work. Can someone point the mistake I am making?
我已经尝试了许多不同的方法来定义函数,作为变量,并使用我在教程中找到的不同语法,尽管这似乎都不起作用。有人能指出我犯的错误吗?
1 个解决方案
#1
5
You need to export the function like this:
你需要导出这样的函数:
function one () {
console.log('one')
};
exports.one = one;
Then it will be accessible from other modules that require it.
然后可以从需要它的其他模块访问它。
(I assume that you use node.js or any other commonjs-like platform)
(我假设你使用node.js或任何其他类似commonjs的平台)
#1
5
You need to export the function like this:
你需要导出这样的函数:
function one () {
console.log('one')
};
exports.one = one;
Then it will be accessible from other modules that require it.
然后可以从需要它的其他模块访问它。
(I assume that you use node.js or any other commonjs-like platform)
(我假设你使用node.js或任何其他类似commonjs的平台)