如何使用其他文件中的函数

时间:2021-03-10 12:20:24

I have 3 files, one as a main.coffee and 2 other files : file1.coffee and file2.coffee with different functions. I want to use the function in my main file (as include in C)

我有3个文件,一个是主文件。咖啡和另外两个文件:file1。咖啡和file2。咖啡有不同的功能。我想在我的主文件中使用这个函数(包括在C中)

main.coffee

main.coffee

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

豁免权1 = function1fromfiles1("hello")豁免权2 = function1fromfiles2("hello")

file1.coffee

file1.coffee

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

function1fromfiles1 =(词)- >控制台。日志file1 +字返回true

file2.coffee

file2.coffee

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

function1fromfiles2 =(词)- >控制台。日志“file2”+词返回true

I tried with require but I have error message:

我尝试了require,但我有错误信息:

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)

ReferenceError: function1fromfiles1在对象上没有定义。 <匿名> (/用户/ admin /文档/ workspace /节点/ file1.coffee:3:1)对象。 <匿名> (/用户/ admin /文档/ workspace /节点/ file1.coffee:1:1)模块。_compile(module.js 456:26):

if someone can help me? thank you

如果有人能帮我?谢谢你!

1 个解决方案

#1


0  

file1.coffee:

file1.coffee:

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

file2.coffee:

file2.coffee:

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

main.coffee:

main.coffee:

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

#1


0  

file1.coffee:

file1.coffee:

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

file2.coffee:

file2.coffee:

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

main.coffee:

main.coffee:

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