CoffeeScript范围问题 [英] CoffeeScript scope issue

查看:95
本文介绍了CoffeeScript范围问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里做错了什么?我使用fat arrow =>我的回调,但是当代码到达cb.onEndElement并调用@returner我得到一个空对象异常。那么为什么@returner不存在?

What am I doing wrong here? I'm using the fat arrows => for my callbacks, however when the code reaches cb.onEndElement and calls @returner I get an null object exception. So why doesn't @returner exist?

class Parser
    constructor: () ->
        @returner = (data) ->

    searchParser: new xml.SaxParser (cb) =>
        cb.onStartElementNS (elem, attrs, prefix, url, ns) =>
            if elem is "results" then @results = []
            else if elem is "title" then @curr = "title"
            else @curr = "none"
        cb.onCdata (cdata) =>
            if @curr is "title" then @book.title = cdata
        cb.onEndElementNS (elem, prefix, url) =>
            @results.push @book if elem is "book"
        cb.onEndDocument =>
            @returner @results

    search: (str, callback) ->
        @returner = callback
        @searchParser.parseString str

p = new Parser
p.search "somexml", (data) ->
    console.log JSON.stringify data


推荐答案

您的方法 search 需要一个fat-arrow => 才能将其绑定到 parser

Your method search needs a fat-arrow => in order to bind it to instances of Parser.

另外,虽然 searchParser:new xml.SaxParser(cb)=& code>编译,它可能不是你想要的,因为胖箭头绑定回调到解析器而不是。您有两个选项:

Additionally, although the line searchParser: new xml.SaxParser (cb) => compiles, it's probably not doing what you want, because the fat arrow is binding the callback to Parser and not this. You have two options:


  1. 您应该可以将 @searchParser = new xml.SaxParser(cb)=& ... 在你的构造函数中,给出你调用它的方式。

  2. 否则您可以使用 searchParser:()=> new xml.SaxParser(cb)=> 并使用下面的括号来调用它: @searchParser()。parseString str searchParser 方法绑定到

  1. you should probably put @searchParser = new xml.SaxParser (cb) => ... in your constructor instead, given the way you are calling it.
  2. otherwise you could use searchParser: () => new xml.SaxParser (cb) => and call it with parens lower down: @searchParser().parseString str, which would create a searchParser method bound to this


$ b b

作为一个例子,下面是我的两个解决方案,以及您的原始行,稍微简化,以及编译代码,用于比较和对比的目的:

As an example, here are my two solutions, as well as your original line, slightly simplified, as well as the compiled code, for compare and contrast purposes:

CoffeeScript中的简化示例:

Simplified example in CoffeeScript:

class Parser
  constructor: () -> @searchParser1 = new xml.SaxParser (x) => console.log(x)
  searchParser2: () => new xml.SaxParser (x) => console.log(x)
  searchParser: new xml.SaxParser (x) => console.log(x)

编译的JavaScript:

Compiled JavaScript:

var Parser;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
Parser = (function() {
  function Parser() {
    this.searchParser2 = __bind(this.searchParser2, this);    
    this.searchParser1 = new xml.SaxParser(__bind(function(x) {
      return console.log(x);
    }, this));
  }
  Parser.prototype.searchParser2 = function() {
    return new xml.SaxParser(__bind(function(x) {
      return console.log(x);
    }, this));
  };
  Parser.prototype.searchParser = new xml.SaxParser(__bind(function(x) {
    return console.log(x);
  }, Parser));
  return Parser;
}).call(this);

请注意 searchParser1 searchParser2 将其回调绑定到 searchParser code> Parser

Note how searchParser1 and searchParser2 have their callbacks bound to this and searchParser's is bound to Parser.

一如往常,CoffeeScript主页上的Try CoffeeScript按钮(http://jashkenas.github .com / coffee-script /)是您的朋友!

As always, the "Try CoffeeScript" button at the CoffeeScript homepage (http://jashkenas.github.com/coffee-script/) is your friend!

这篇关于CoffeeScript范围问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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