How to properly initialize an ErrorEvent in javascript?

五迷三道 提交于 2019-12-10 17:19:22

问题


I need to fire an ErrorEvent programmatically, but can't figure out how to initialize the properties of the event. The properties are readonly, and initEvent() only initializes the event type, whether it bubbles, and if it's cancellable.

I tried

var myErrorEvent = new ErrorEvent("error", new Error("This is my error"));

and

myErrorEvent.initEvent("error", false, true, new Error("This is my error"));

or

// This function would be defined in IE only. Could not find it in Chrome.
myErrorEvent.initErrorEvent("error", false, true, "This is my error", "myfile.js", 1234);

I tried many other things, but could not get the properties of the ErrorEvent to be set properly.

Is this even feasible?


回答1:


You were close! The 2nd argument to the ErrorEvent constructor is an object with the properties you want to set, like this:

var error = new ErrorEvent('oh nose', {
    error : new Error('AAAHHHH'),
    message : 'A monkey is throwing bananas at me!',
    lineno : 402,
    filename : 'closet.html'
});

For the full set of properties, see the spec.



来源:https://stackoverflow.com/questions/26554607/how-to-properly-initialize-an-errorevent-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!