How to properly initialize ErrorEvent in javascript?

I need to run ErrorEventprogrammatically, but cannot figure out how to initialize the properties of the event. Properties are read-only, and initEvent()initializes the type of event, regardless of whether it is a bubble, and if it is canceled.

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 set properties properly ErrorEvent.

Is it possible?

+3
source share
1 answer

You were close! The second argument to the constructor ErrorEventis the object with the properties you want to set, for example:

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

For a complete set of properties, see the specification .

+9

Source: https://habr.com/ru/post/1662657/


All Articles