【问题标题】:How to use functions from an other files.coffee如何使用其他文件中的函数.coffee
【发布时间】:2014-12-25 00:34:17
【问题描述】:

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

主咖啡

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

我尝试使用 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)

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

【问题讨论】:

    标签: javascript node.js coffeescript include require


    【解决方案1】:

    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")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-25
      • 1970-01-01
      • 1970-01-01
      • 2013-08-15
      • 1970-01-01
      相关资源
      最近更新 更多