匿名函数作为回调,但定义的函数不 [英] Anonymous function works as callback but defined function does not

查看:237
本文介绍了匿名函数作为回调,但定义的函数不的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似这样的模块:

  module.exports = AtomMarkdownLabels = 


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

process_yaml:(err,data)=>
console.logprocess_yaml被称为

我知道 file_changed_added 正在从一些其他函数调用,我看到控制台中的文件已更改输出,但 process_yaml 不是如果我更改 file_changed_added

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

我看到正确调用测试和文件更改。可能发生什么?

解决方案

=> 有两个略有不同的用途:


  1. 定义命名函数( f = => ... )或匿名函数 f(x,=> ...)), => c $ c> @ 与周围环境中的 @ 相同。


  2. 在类中定义方法时:

      class C 
    m:=> ...

    => @ <将是 C 的实例。 / p>


这两个用法都是创建一个绑定函数,但它们绑定到不同的东西。



您正在使用此结构:

  obj = 
func:=&
#...

这相当于:

  f = => 
#...
obj =
func:f

因为你使用一个普通的旧对象而不是一个类。那么 AtomMarkdownLabels 定义之外的是什么? @ @ 不会是有用的,特别是它不会是你的 AtomMarkdownLabels 对象,有 process_yaml 属性,因此 @process_yaml 内部 file_changed_added 未定义或错误。



我不知道Atom希望你返回什么,应该工作,像这样:

 #使用类,以便=> gt;做你期望它做的事
class AtomMarkdownLabels
#其他的东西在这里

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

#你可能需要或可能不需要=>这里
process_yaml:(err,data)=>
console.logprocess_yaml被调用

#导出类的实例
module.exports = new AtomMarkdownLabels

如果你想要或必须使用一个普通对象,那么你可以绕过 @

 #只是简单的旧函数,所以这样定义
process_yaml =(err,data)
console.logprocess_yaml被调用

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

module.exports = AtomMarkdownLabels =
b
$ b file_changed_added:file_changed_added

或这样:

 #明确命名你的对象,而不是使用@ 
module.exports = AtomMarkdownLabels =
#其他的东西在这里

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

process_yaml:(err,data) - >
console.logprocess_yaml被称为






这应该可以解决几天前您的其他 CoffeeScript问题。


I have a module that looks like this:

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"

I know file_changed_added is being called from some other function and I'm seeing the "file changed" output in the console, but process_yaml isn't if I change file_changed_added to

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

I see both "test" and "file changed" being called properly. What could be going on?

解决方案

=> has two slightly different purposes:

  1. When defining a named function (f = => ...) or anonymous function f(x, => ...)), => simply ensures that @ inside the function is that same as @ in the surrounding context.

  2. When defining a method in a class:

    class C
      m: => ...
    

    => ensures that @ inside m will be the instance of C.

Both uses are creating a bound function but they're binding to different things.

You're using this structure:

obj =
  func: =>
    # ...

That's equivalent to this:

f = =>
  # ...
obj =
  func: f

because you're using a plain old object rather than a class. So what is @ outside your AtomMarkdownLabels definition? @ won't be anything useful and in particular, it won't be your AtomMarkdownLabels object and it won't have a process_yaml property so @process_yaml inside file_changed_added is going to be undefined or an error.

I'm not sure what specifically Atom wants you to return but a class should work, something like this:

# 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

If you want to or must use a plain object then you could bypass @ completely and do it like this:

# 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

or like this:

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


This should solve your other CoffeeScript problem from a few days ago too.

这篇关于匿名函数作为回调,但定义的函数不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆