Coffeescript 匿名函数用作回调函数,但定义的函数不用作回调函数

Coffeescript 匿名函数用作回调函数,但定义的函数不用作回调函数,coffeescript,Coffeescript,我有一个模块,如下所示: module.exports = AtomMarkdownLabels = # other stuff here file_changed_added: (file_path) => fs.readFile file_path, 'utf-8', @process_yaml console.log 'file changed' process_yaml: (err, data) => console.log "proc

我有一个模块,如下所示:

module.exports = AtomMarkdownLabels =
  # other stuff here

  file_changed_added: (file_path) =>
    fs.readFile file_path, 'utf-8', @process_yaml
    console.log 'file changed'

  process_yaml: (err, data) =>
    console.log "process_yaml is called"
# Use a class so that => does what you're expecting it to do
class AtomMarkdownLabels
  # other stuff here

  file_changed_added: (file_path) =>
    fs.readFile file_path, 'utf-8', @process_yaml
    console.log 'file changed'

  # you may or may not need => here
  process_yaml: (err, data) =>
    console.log "process_yaml is called"

# Export an instance of your class
module.exports = new AtomMarkdownLabels
# Just plain old functions so define them that way
process_yaml = (err, data) ->
  console.log "process_yaml is called"

file_changed_added = (file_path) ->
  fs.readFile file_path, 'utf-8', process_yaml
  console.log 'file changed'

module.exports = AtomMarkdownLabels =
  # other stuff here

  file_changed_added: file_changed_added
# Explicitly name your object rather than using @
module.exports = AtomMarkdownLabels =
  # other stuff here

  file_changed_added: (file_path) ->
    fs.readFile file_path, 'utf-8', AtomMarkdownLabels.process_yaml
    console.log 'file changed'

  process_yaml: (err, data) ->
    console.log "process_yaml is called"
我知道从其他函数调用了
file\u changed\u added
,我在控制台中看到了“file changed”输出,但是如果我将
file\u changed\u added
更改为

file_changed_added: (file_path) =>
    fs.readFile file_path, 'utf-8', (err, data) =>
      console.log "test"
    console.log 'file changed'

我看到“测试”和“文件更改”都被正确调用。会发生什么事

=>
有两个稍微不同的用途:

  • 定义命名函数(
    f==>…
    )或匿名函数
    f(x,=>…)
    )时,
    =>
    只需确保函数内部的
    @
    与周围上下文中的
    @
    相同

  • 在类中定义方法时:

    class C
      m: => ...
    
    =>
    确保
    @
    内部的
    m
    将是
    C
    的实例

  • 这两种用法都在创建绑定函数,但它们绑定到不同的对象

    您正在使用此结构:

    obj =
      func: =>
        # ...
    
    这相当于:

    f = =>
      # ...
    obj =
      func: f
    
    因为您使用的是普通的旧对象,而不是类。那么,在您的
    AtomMarkdownLabels
    定义之外的
    @
    是什么
    @
    不会有任何用处,特别是,它不会是您的
    AtomMarkdownLabels
    对象,也不会有
    process\u yaml
    属性,因此
    @process\u yaml
    中添加的
    文件将是
    未定义的
    或错误

    我不确定Atom具体希望您返回什么,但类应该可以工作,如下所示:

    module.exports = AtomMarkdownLabels =
      # other stuff here
    
      file_changed_added: (file_path) =>
        fs.readFile file_path, 'utf-8', @process_yaml
        console.log 'file changed'
    
      process_yaml: (err, data) =>
        console.log "process_yaml is called"
    
    # Use a class so that => does what you're expecting it to do
    class AtomMarkdownLabels
      # other stuff here
    
      file_changed_added: (file_path) =>
        fs.readFile file_path, 'utf-8', @process_yaml
        console.log 'file changed'
    
      # you may or may not need => here
      process_yaml: (err, data) =>
        console.log "process_yaml is called"
    
    # Export an instance of your class
    module.exports = new AtomMarkdownLabels
    
    # Just plain old functions so define them that way
    process_yaml = (err, data) ->
      console.log "process_yaml is called"
    
    file_changed_added = (file_path) ->
      fs.readFile file_path, 'utf-8', process_yaml
      console.log 'file changed'
    
    module.exports = AtomMarkdownLabels =
      # other stuff here
    
      file_changed_added: file_changed_added
    
    # Explicitly name your object rather than using @
    module.exports = AtomMarkdownLabels =
      # other stuff here
    
      file_changed_added: (file_path) ->
        fs.readFile file_path, 'utf-8', AtomMarkdownLabels.process_yaml
        console.log 'file changed'
    
      process_yaml: (err, data) ->
        console.log "process_yaml is called"
    
    如果您想要或必须使用普通对象,则可以完全绕过
    @
    ,如下操作:

    module.exports = AtomMarkdownLabels =
      # other stuff here
    
      file_changed_added: (file_path) =>
        fs.readFile file_path, 'utf-8', @process_yaml
        console.log 'file changed'
    
      process_yaml: (err, data) =>
        console.log "process_yaml is called"
    
    # Use a class so that => does what you're expecting it to do
    class AtomMarkdownLabels
      # other stuff here
    
      file_changed_added: (file_path) =>
        fs.readFile file_path, 'utf-8', @process_yaml
        console.log 'file changed'
    
      # you may or may not need => here
      process_yaml: (err, data) =>
        console.log "process_yaml is called"
    
    # Export an instance of your class
    module.exports = new AtomMarkdownLabels
    
    # Just plain old functions so define them that way
    process_yaml = (err, data) ->
      console.log "process_yaml is called"
    
    file_changed_added = (file_path) ->
      fs.readFile file_path, 'utf-8', process_yaml
      console.log 'file changed'
    
    module.exports = AtomMarkdownLabels =
      # other stuff here
    
      file_changed_added: file_changed_added
    
    # Explicitly name your object rather than using @
    module.exports = AtomMarkdownLabels =
      # other stuff here
    
      file_changed_added: (file_path) ->
        fs.readFile file_path, 'utf-8', AtomMarkdownLabels.process_yaml
        console.log 'file changed'
    
      process_yaml: (err, data) ->
        console.log "process_yaml is called"
    
    或者像这样:

    module.exports = AtomMarkdownLabels =
      # other stuff here
    
      file_changed_added: (file_path) =>
        fs.readFile file_path, 'utf-8', @process_yaml
        console.log 'file changed'
    
      process_yaml: (err, data) =>
        console.log "process_yaml is called"
    
    # Use a class so that => does what you're expecting it to do
    class AtomMarkdownLabels
      # other stuff here
    
      file_changed_added: (file_path) =>
        fs.readFile file_path, 'utf-8', @process_yaml
        console.log 'file changed'
    
      # you may or may not need => here
      process_yaml: (err, data) =>
        console.log "process_yaml is called"
    
    # Export an instance of your class
    module.exports = new AtomMarkdownLabels
    
    # Just plain old functions so define them that way
    process_yaml = (err, data) ->
      console.log "process_yaml is called"
    
    file_changed_added = (file_path) ->
      fs.readFile file_path, 'utf-8', process_yaml
      console.log 'file changed'
    
    module.exports = AtomMarkdownLabels =
      # other stuff here
    
      file_changed_added: file_changed_added
    
    # Explicitly name your object rather than using @
    module.exports = AtomMarkdownLabels =
      # other stuff here
    
      file_changed_added: (file_path) ->
        fs.readFile file_path, 'utf-8', AtomMarkdownLabels.process_yaml
        console.log 'file changed'
    
      process_yaml: (err, data) ->
        console.log "process_yaml is called"
    


    这也可以解决几天前的咖啡脚本问题。

    =>
    有两个稍微不同的用途:

  • 定义命名函数(
    f==>…
    )或匿名函数
    f(x,=>…)
    )时,
    =>
    只需确保函数内部的
    @
    与周围上下文中的
    @
    相同

  • 在类中定义方法时:

    class C
      m: => ...
    
    =>
    确保
    @
    内部的
    m
    将是
    C
    的实例

  • 这两种用法都在创建绑定函数,但它们绑定到不同的对象

    您正在使用此结构:

    obj =
      func: =>
        # ...
    
    这相当于:

    f = =>
      # ...
    obj =
      func: f
    
    因为您使用的是普通的旧对象,而不是类。那么,在您的
    AtomMarkdownLabels
    定义之外的
    @
    是什么
    @
    不会有任何用处,特别是,它不会是您的
    AtomMarkdownLabels
    对象,也不会有
    process\u yaml
    属性,因此
    @process\u yaml
    中添加的
    文件将是
    未定义的
    或错误

    我不确定Atom具体希望您返回什么,但类应该可以工作,如下所示:

    module.exports = AtomMarkdownLabels =
      # other stuff here
    
      file_changed_added: (file_path) =>
        fs.readFile file_path, 'utf-8', @process_yaml
        console.log 'file changed'
    
      process_yaml: (err, data) =>
        console.log "process_yaml is called"
    
    # Use a class so that => does what you're expecting it to do
    class AtomMarkdownLabels
      # other stuff here
    
      file_changed_added: (file_path) =>
        fs.readFile file_path, 'utf-8', @process_yaml
        console.log 'file changed'
    
      # you may or may not need => here
      process_yaml: (err, data) =>
        console.log "process_yaml is called"
    
    # Export an instance of your class
    module.exports = new AtomMarkdownLabels
    
    # Just plain old functions so define them that way
    process_yaml = (err, data) ->
      console.log "process_yaml is called"
    
    file_changed_added = (file_path) ->
      fs.readFile file_path, 'utf-8', process_yaml
      console.log 'file changed'
    
    module.exports = AtomMarkdownLabels =
      # other stuff here
    
      file_changed_added: file_changed_added
    
    # Explicitly name your object rather than using @
    module.exports = AtomMarkdownLabels =
      # other stuff here
    
      file_changed_added: (file_path) ->
        fs.readFile file_path, 'utf-8', AtomMarkdownLabels.process_yaml
        console.log 'file changed'
    
      process_yaml: (err, data) ->
        console.log "process_yaml is called"
    
    如果您想要或必须使用普通对象,则可以完全绕过
    @
    ,如下操作:

    module.exports = AtomMarkdownLabels =
      # other stuff here
    
      file_changed_added: (file_path) =>
        fs.readFile file_path, 'utf-8', @process_yaml
        console.log 'file changed'
    
      process_yaml: (err, data) =>
        console.log "process_yaml is called"
    
    # Use a class so that => does what you're expecting it to do
    class AtomMarkdownLabels
      # other stuff here
    
      file_changed_added: (file_path) =>
        fs.readFile file_path, 'utf-8', @process_yaml
        console.log 'file changed'
    
      # you may or may not need => here
      process_yaml: (err, data) =>
        console.log "process_yaml is called"
    
    # Export an instance of your class
    module.exports = new AtomMarkdownLabels
    
    # Just plain old functions so define them that way
    process_yaml = (err, data) ->
      console.log "process_yaml is called"
    
    file_changed_added = (file_path) ->
      fs.readFile file_path, 'utf-8', process_yaml
      console.log 'file changed'
    
    module.exports = AtomMarkdownLabels =
      # other stuff here
    
      file_changed_added: file_changed_added
    
    # Explicitly name your object rather than using @
    module.exports = AtomMarkdownLabels =
      # other stuff here
    
      file_changed_added: (file_path) ->
        fs.readFile file_path, 'utf-8', AtomMarkdownLabels.process_yaml
        console.log 'file changed'
    
      process_yaml: (err, data) ->
        console.log "process_yaml is called"
    
    或者像这样:

    module.exports = AtomMarkdownLabels =
      # other stuff here
    
      file_changed_added: (file_path) =>
        fs.readFile file_path, 'utf-8', @process_yaml
        console.log 'file changed'
    
      process_yaml: (err, data) =>
        console.log "process_yaml is called"
    
    # Use a class so that => does what you're expecting it to do
    class AtomMarkdownLabels
      # other stuff here
    
      file_changed_added: (file_path) =>
        fs.readFile file_path, 'utf-8', @process_yaml
        console.log 'file changed'
    
      # you may or may not need => here
      process_yaml: (err, data) =>
        console.log "process_yaml is called"
    
    # Export an instance of your class
    module.exports = new AtomMarkdownLabels
    
    # Just plain old functions so define them that way
    process_yaml = (err, data) ->
      console.log "process_yaml is called"
    
    file_changed_added = (file_path) ->
      fs.readFile file_path, 'utf-8', process_yaml
      console.log 'file changed'
    
    module.exports = AtomMarkdownLabels =
      # other stuff here
    
      file_changed_added: file_changed_added
    
    # Explicitly name your object rather than using @
    module.exports = AtomMarkdownLabels =
      # other stuff here
    
      file_changed_added: (file_path) ->
        fs.readFile file_path, 'utf-8', AtomMarkdownLabels.process_yaml
        console.log 'file changed'
    
      process_yaml: (err, data) ->
        console.log "process_yaml is called"
    


    这也可以解决几天前的咖啡脚本问题。

    谢谢您的详细解释。我相信你是对的,这就是我之前遇到的问题的原因。谢谢你的详细解释。我相信你是对的,这就是我之前遇到的问题的原因。