如何使用其他files.coffee中的函数

时间:2014-10-29 10:34:12

标签: javascript node.js coffeescript include require

我有3个文件,一个作为main.coffee,另外两个文件:file1.coffee和file2.coffee具有不同的功能。我想在我的主文件中使用该函数(包含在C中)

main.coffee

exemple1 = function1fromfiles1("hello") exemple2 = function1fromfiles2("hello")

file1.coffee

function1fromfiles1=(word)-> console.log "file1"+word return true

file2.coffee

function1fromfiles2=(word)-> console.log "file2"+word return true

我尝试了但是我有错误信息:

ReferenceError: function1fromfiles1 is not defined at Object.<anonymous> (/Users/admin/Documents/workspace/node/file1.coffee:3:1) at Object.<anonymous> (/Users/admin/Documents/workspace/node/file1.coffee:1:1) at Module._compile (module.js:456:26)

如果有人可以帮助我?谢谢

1 个答案:

答案 0 :(得分:0)

file1.coffee:

module.exports = (word)->
  console.log "file1"+word
  return true

file2.coffee:

module.exports = (word)->
  console.log "file2"+word
  return true

main.coffee:

function1fromfiles1 = require('./file1.coffee')
function1fromfiles2 = require('./file2.coffee')
exemple1 = function1fromfiles1("hello")
exemple2 = function1fromfiles2("hello")