如何确定变量是否是javascript中的错误?
我在一个数组中混合了几个变量,其中一些可能是一般错误或自定义错误,即由var v=new Error或var v=new MyCustomError生成 是否有一种通用的方法来区分错误实例和任何其他变量?谢谢 编辑:自定义错误的格式为: 函数FacebookApiExceptionres{ this.name=FacebookApiException; this.message=JSON.stringifyres | |{}; 这个响应=res; } FacebookApiException.prototype=Error.prototype;如何确定变量是否是javascript中的错误?,javascript,prototype,typeof,Javascript,Prototype,Typeof,我在一个数组中混合了几个变量,其中一些可能是一般错误或自定义错误,即由var v=new Error或var v=new MyCustomError生成 是否有一种通用的方法来区分错误实例和任何其他变量?谢谢 编辑:自定义错误的格式为: 函数FacebookApiExceptionres{ this.name=FacebookApiException; this.message=JSON.stringifyres | |{}; 这个响应=res; } FacebookApiException.p
在大多数情况下,您可以使用instanceof运算符,例如
var err = new Error();
if (err instanceof Error) {
alert('That was an error!');
}
看这个
Mozilla开发者网络MDN提供了有关instanceof operator的更多详细信息,说明:
instanceof操作符测试对象原型链中是否存在constructor.prototype
这样,考虑到以下输入,您将获得评论中反映的输出:
function C(){} // defining a constructor
function D(){} // defining another constructor
var o = new C();
o instanceof C; // true, because: Object.getPrototypeOf(o) === C.prototype
o instanceof D; // false, because D.prototype is nowhere in o's prototype chain
o instanceof Object; // true, because:
C.prototype instanceof Object // true
C.prototype = {};
var o2 = new C();
o2 instanceof C; // true
o instanceof C; // false, because C.prototype is nowhere in o's prototype chain anymore
D.prototype = new C(); // use inheritance
var o3 = new D();
o3 instanceof D; // true
o3 instanceof C; // true
在大多数情况下,您可以使用instanceof运算符,例如
var err = new Error();
if (err instanceof Error) {
alert('That was an error!');
}
看这个
Mozilla开发者网络MDN提供了有关instanceof operator的更多详细信息,说明:
instanceof操作符测试对象原型链中是否存在constructor.prototype
这样,考虑到以下输入,您将获得评论中反映的输出:
function C(){} // defining a constructor
function D(){} // defining another constructor
var o = new C();
o instanceof C; // true, because: Object.getPrototypeOf(o) === C.prototype
o instanceof D; // false, because D.prototype is nowhere in o's prototype chain
o instanceof Object; // true, because:
C.prototype instanceof Object // true
C.prototype = {};
var o2 = new C();
o2 instanceof C; // true
o instanceof C; // false, because C.prototype is nowhere in o's prototype chain anymore
D.prototype = new C(); // use inheritance
var o3 = new D();
o3 instanceof D; // true
o3 instanceof C; // true
您的FacebookApiException无法从错误中正确继承,我建议您这样做:
function FacebookApiException(res) {
//re use Error constructor
Error.call(this,JSON.stringify(res || {}));
this.name = "FacebookApiException";
this.response = res;
}
//Faceb... is an Error but Error is not Faceb...
// so you can't set it's prototype to be equal to Error
FacebookApiException.prototype = Object.create(Error.prototype);
//for completeness, not strictly needed but prototype.constructor
// is there automatically and should point to the right constructor
// re assigning the prototype has it pointing to Error but should be
// FacebookApiException
FacebookApiException.prototype.constructor = FacebookApiException;
您的FacebookApiException无法从错误中正确继承,我建议您这样做:
function FacebookApiException(res) {
//re use Error constructor
Error.call(this,JSON.stringify(res || {}));
this.name = "FacebookApiException";
this.response = res;
}
//Faceb... is an Error but Error is not Faceb...
// so you can't set it's prototype to be equal to Error
FacebookApiException.prototype = Object.create(Error.prototype);
//for completeness, not strictly needed but prototype.constructor
// is there automatically and should point to the right constructor
// re assigning the prototype has it pointing to Error but should be
// FacebookApiException
FacebookApiException.prototype.constructor = FacebookApiException;
instanceof会的。请给我们看一下我的自定义错误,好吗?@Evan请把它添加到您的问题中谢谢大家,instanceof会的。instanceof会的。请给我们看一下我的自定义错误,好吗?@Evan请把它添加到您的问题中谢谢大家,instanceof就是这样。答案的第二部分很重要:instanceof是正常的,前提是你知道[[Prototype]]链没有被篡改,并且对象可以是多个构造函数的实例。答案的第二部分很重要:instanceof是正常的,前提是你知道[[Prototype]]链没有被篡改,对象可以是多个构造函数的实例。非常有趣,谢谢你指出这一点!我真的很高兴你让我发现Object.create.OP也可以执行FacebookApiException.prototype=new Error并重置构造函数属性,但是上面的方法是更好的选择。非常有趣,谢谢你指出这一点!我真的很高兴您让我发现Object.create.OP还可以执行FacebookApiException.prototype=new Error并重置构造函数属性,但上面的方法是更好的选择。