问题
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