Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 未设置错误的子类中的属性_Node.js_Coffeescript - Fatal编程技术网

Node.js 未设置错误的子类中的属性

Node.js 未设置错误的子类中的属性,node.js,coffeescript,Node.js,Coffeescript,我有一个Coffeescript子类Error。下面的咖啡脚本 class MyError extends Error constructor: (message, @cause) -> super message myError = new MyError 'This is the error message!' console.log myError instanceof MyError console.log myError instanceof Error cons

我有一个Coffeescript子类
Error
。下面的咖啡脚本

class MyError extends Error
  constructor: (message, @cause) ->
    super message

myError = new MyError 'This is the error message!'

console.log myError instanceof MyError
console.log myError instanceof Error
console.log "'#{myError.message}'"
console.log myError.message == ''
显示

true
true
''
true
在Node.js v0.10.20下


为什么
myError
的message属性为空?

显式设置
@message
有效

class MyError extends Error
  constructor: (@message,@cause)->
    Error.captureStackTrace(@,@)

coffee> ee=new MyError 'test'
{ message: 'test', cause: undefined }
coffee> "#{ee}"
'Error: test'
coffee> ee.message
'test'
coffee> ee instanceof MyError
true
coffee> ee instanceof Error
true
coffee> throw new MyError 'test'
Error: test
    at new MyError (repl:10:11)
...
super
在另一个类构建于
MyError

class OError extends MyError
   constructor: (msg)->
     super msg
     @name='OError'
以下显示正确的消息,适用于
util.isError
,是
instanceof Error
,但不是
instanceof Error 1
。因此,它是
错误的专门构造函数,而不是“子类”

class Error1 extends Error
   constructor: (@message)->
     self = super
     self.name = 'Error1'
     return self
这适用于
节点:“0.10.1”,“咖啡脚本”:“1.6.3”

bjb.io
文章中的最后一个例子是(在Coffeescript中):


这满足了所有测试,
util.isError
instanceof Error
instanceof CustomError
“#{new CustomError'xxx'}”
看看这个问题,问题在于错误构造函数的返回值。谢谢。在我发布这个问题之前,我已经看到了这些链接。在阅读了所有这些问题和评论之后,在尝试了
示例的instanceof
之后(据我所见,这些示例都运行正常),我认为子类化没有问题。知道
util.isError(myError)
在Node.js下返回
false
,我的问题的答案是。谢谢你的详细回答。它证实了我的结论,即在CS中对
Error
进行子类化不会产生一个满足我所有要求的类:correct
instanceof
、correct properties和Node.js下的
util.isError ee==true
。读过“”之后,我不再考虑子类化
Error
。谢谢添加!我的问题现在得到了完全的回答!据我所知,
CustomError
满足所有测试,但不是真正的子类。我检查了
类CustomError2扩展CustomError…
是否不满足所有测试。
CustomError = (msg)->
  self = new Error msg
  self.name = 'CustomError'
  self.__proto__ = CustomError.prototype
  return self
CustomError.prototype.__proto__= Error.prototype
# CustomError::__proto__= Error::  # I think