CoffeeScript 类扩展 - TypeError: Object #没有“执行”方法;
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CD Sandchez 的评论是正确的。如果删除
sys.inherits
行,则perform
运行。你应该做的是一直使用 CoffeeScript 继承:
CD Sandchez's comment is correct. If you cut out the
sys.inherits
line, thenperform
runs.What you should do instead is use CoffeeScript inheritance all the way: