gpt4 book ai didi

javascript - CoffeeScript 类扩展 - TypeError : Object # has no method 'perform'

转载 作者:行者123 更新时间:2023-11-30 13:33:37 26 4
gpt4 key购买 nike

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 没有方法‘perform’”。 extend 不应该提供那个方法吗?

更新

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

{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

最佳答案

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

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

class Search extends process.EventEmitter

关于javascript - CoffeeScript 类扩展 - TypeError : Object #<Google> has no method 'perform' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5771856/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com