Nodejs +需要instanceof行为 [英] Nodejs + Require instanceof behaviour

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

问题描述

我在NodeJ中遇到了一个奇怪的行为与instanceof。我正在尝试创建一个模块,它抛出可以通过使用模块捕获的异常并基于异常类型进行处理。然而,测试类型 mod.MyException 的错误实例的调用总是返回false。



mymodule.js

  function MyException(){} 
MyException.prototype = new Error();
MyException.prototype.constructor = MyException;

函数Foo(){
throw new MyException();
}

exports.Foo = Foo;
exports.MyException = MyException;

myconsumingmodule.js

  var mod = require('mymodule'); 

try {
mod.Foo();
} catch(error){
if(error instanceof mod.MyException){// always return false
// do error specific handling(never execution)
return;
}
//通用处理
}

如果我在mymodule.js中的测试的例子像这样返回true



mymodule.js

  var a = new MyException()
console.log(一个instanceof MyException); // true

但是,如果我在消费模块中创建一个异常并进行测试,它会将其正确识别为MyException,它只是在与其他模块创建的异常进行比较时不会。



myconsumingmodule.js

  var mod = require('mymodule')
var b = new mod.MyException();
console.log(b instanceof mod.MyException); // true


解决方案

发现它



要使用你的例子,在文件 one.js 我有:

  var mod = require('mymodule'); 

但在另一个文件 / code>我有:

  var mod = require('Mymodule'); 

除了当你比较从 one.js 使用 instanceof two.js



Absurdly,node.js允许你做一个不区分大小写的require,除了它不是真的不区分大小写,它实际上需要你的文件两次 c $ c> $ = - 构造函数是两个不同的模块实例的两个不同的函数!



TL; DR



确保您所有的要求字符串是一致的。


I'm encountering a strange behaviour with instanceof in NodeJs. I'm trying to creating a module which throws exceptions which can be caught by consuming modules and dealt with based on the exception type. However the call to test the type "error instanceof mod.MyException" is always returning false.

mymodule.js:

function MyException() {}
MyException.prototype = new Error();
MyException.prototype.constructor = MyException;

function Foo() {
    throw new MyException();
}

exports.Foo = Foo;
exports.MyException = MyException;

myconsumingmodule.js

var mod = require('mymodule');

try {
    mod.Foo();
} catch (error) {
    if (error instanceof mod.MyException) { // always returns false
         //do error specific handling (never executes)
         return;
    }
    // generic handling
}

If I do the instanceof test in mymodule.js like so it returns true

mymodule.js

var a = new MyException()
console.log(a instanceof MyException); //true

However if I create an exception in the consuming module and test it it correctly identifies it as MyException, it just doesn't when comparing it against an exception that was created by the other module.

myconsumingmodule.js

var mod = require('mymodule')
var b = new mod.MyException();
console.log(b instanceof mod.MyException); // true

解决方案

FOUND IT

To use your example, in file one.js I had:

var mod = require('mymodule');

but in another file two.js I had:

var mod = require('Mymodule');

and everything seemed to work exactly the same, except when you compared an error created from one.js using instanceof in two.js!.

Absurdly, node.js allows you to do a case-insensitive require, except it's not really case-insensitive, it actually requires your file twice, which is why instanceof returns false – the constructors are two different functions from two different module instances!

TL;DR

Make sure all your require strings are case-consistent.

这篇关于Nodejs +需要instanceof行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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