CoffeeScript 类扩展 - TypeError: Object #没有“执行”方法;

发布于 2024-11-03 09:42:42 字数 2483 浏览 1 评论 0原文

class Search
  perform: (text) ->
    query = encodeURIComponent text
    results = ''
    request {uri: @uri + query }, (error, response, body) =>
      if !error? && response.statusCode == 200
        window = jsdom.jsdom(body).createWindow()
        jsdom.jQueryify window, './jquery-1.5.min.js', (window, jquery) =>
          $ = window.$
          jquery(@pattern).each (i, lmn) =>
            link = $(lmn).text() + " - " +  $(lmn).attr('href') 
            if requiresHttp? && /^http/.exec  $(lmn).attr('href')
              results = results + link + "\n"
            else
              results = results + link + "\n"
          @emit 'end', results

sys.inherits(Search, process.EventEmitter)

class Google extends Search
  @uri     = 'http://www.google.com/search?q='
  @pattern = '#ires ol li .r a'

当我执行时,

google = new Google
google.perform 'blah'

它返回错误“TypeError: Object #Google has no method 'perform'”。扩展不应该提供该方法吗?

更新

发布后不久,我重构了代码。这是工作结果:

{EventEmitter} = require "events"
request        = require 'request'
jsdom          = require 'jsdom'


class Search extends EventEmitter
  constructor: ->
    @linkPrefix   = ''
    @requiresHttp = false
    @uri          = ''
    @pattern      = ''
  perform: (text) ->
    query = encodeURIComponent text
    results = ''
    request {uri: @uri + query }, (error, response, body) =>
      if !error? && response.statusCode == 200
        window = jsdom.jsdom(body).createWindow()
        jsdom.jQueryify window, './jquery-1.5.min.js', (window, jquery) =>
          $ = window.$
          jquery(@pattern).each (i, lmn) =>
            link = $(lmn).text() + " - " + @linkPrefix + $(lmn).attr('href') 
            if @requiresHttp?
              if /^http/.exec  $(lmn).attr('href')
                results = results + link + "\n"
            else
              results = results + link + "\n"
          @emit 'end', results

class Google extends Search
  constructor: ->
    @uri          = 'http://www.google.com/search?q='
    @pattern      = '#ires ol li .r a'
    @requiresHttp = true

class Youtube extends Search
  constructor: ->
    @uri          = 'http://www.youtube.com/results?search_query='
    @pattern      = '#search-results h3 a'
    @linkPrefix   = 'http://www.youtube.com'


exports.Google  = Google
exports.Youtube = Youtube
class Search
  perform: (text) ->
    query = encodeURIComponent text
    results = ''
    request {uri: @uri + query }, (error, response, body) =>
      if !error? && response.statusCode == 200
        window = jsdom.jsdom(body).createWindow()
        jsdom.jQueryify window, './jquery-1.5.min.js', (window, jquery) =>
          $ = window.$
          jquery(@pattern).each (i, lmn) =>
            link = $(lmn).text() + " - " +  $(lmn).attr('href') 
            if requiresHttp? && /^http/.exec  $(lmn).attr('href')
              results = results + link + "\n"
            else
              results = results + link + "\n"
          @emit 'end', results

sys.inherits(Search, process.EventEmitter)

class Google extends Search
  @uri     = 'http://www.google.com/search?q='
  @pattern = '#ires ol li .r a'

When I execute

google = new Google
google.perform 'blah'

It's returning the error "TypeError: Object #Google has no method 'perform'". Shouldn't the extend provide that method?

UPDATE

Shortly after posting I re-factored the code. Here is the working result:

{EventEmitter} = require "events"
request        = require 'request'
jsdom          = require 'jsdom'


class Search extends EventEmitter
  constructor: ->
    @linkPrefix   = ''
    @requiresHttp = false
    @uri          = ''
    @pattern      = ''
  perform: (text) ->
    query = encodeURIComponent text
    results = ''
    request {uri: @uri + query }, (error, response, body) =>
      if !error? && response.statusCode == 200
        window = jsdom.jsdom(body).createWindow()
        jsdom.jQueryify window, './jquery-1.5.min.js', (window, jquery) =>
          $ = window.$
          jquery(@pattern).each (i, lmn) =>
            link = $(lmn).text() + " - " + @linkPrefix + $(lmn).attr('href') 
            if @requiresHttp?
              if /^http/.exec  $(lmn).attr('href')
                results = results + link + "\n"
            else
              results = results + link + "\n"
          @emit 'end', results

class Google extends Search
  constructor: ->
    @uri          = 'http://www.google.com/search?q='
    @pattern      = '#ires ol li .r a'
    @requiresHttp = true

class Youtube extends Search
  constructor: ->
    @uri          = 'http://www.youtube.com/results?search_query='
    @pattern      = '#search-results h3 a'
    @linkPrefix   = 'http://www.youtube.com'


exports.Google  = Google
exports.Youtube = Youtube

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧城烟雨 2024-11-10 09:42:42

CD Sandchez 的评论是正确的。如果删除 sys.inherits 行,则 perform 运行。

你应该做的是一直使用 CoffeeScript 继承:

class Search extends process.EventEmitter

CD Sandchez's comment is correct. If you cut out the sys.inherits line, then perform runs.

What you should do instead is use CoffeeScript inheritance all the way:

class Search extends process.EventEmitter
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文