mirror of
https://github.com/denoland/deno
synced 2024-08-27 20:09:17 +00:00
Avoid search prototype chain (#2019)
This commit is contained in:
parent
3d6c033369
commit
51c6f33f64
|
@ -14,7 +14,7 @@ export class EventTarget implements domTypes.EventTarget {
|
|||
listener: domTypes.EventListenerOrEventListenerObject | null,
|
||||
_options?: boolean | domTypes.AddEventListenerOptions
|
||||
): void {
|
||||
if (!(type in this.listeners)) {
|
||||
if (!this.listeners.hasOwnProperty(type)) {
|
||||
this.listeners[type] = [];
|
||||
}
|
||||
if (listener !== null) {
|
||||
|
@ -27,7 +27,7 @@ export class EventTarget implements domTypes.EventTarget {
|
|||
callback: domTypes.EventListenerOrEventListenerObject | null,
|
||||
_options?: domTypes.EventListenerOptions | boolean
|
||||
): void {
|
||||
if (type in this.listeners && callback !== null) {
|
||||
if (this.listeners.hasOwnProperty(type) && callback !== null) {
|
||||
this.listeners[type] = this.listeners[type].filter(
|
||||
listener => listener !== callback
|
||||
);
|
||||
|
@ -35,7 +35,7 @@ export class EventTarget implements domTypes.EventTarget {
|
|||
}
|
||||
|
||||
public dispatchEvent(event: domTypes.Event): boolean {
|
||||
if (!(event.type in this.listeners)) {
|
||||
if (!this.listeners.hasOwnProperty(event.type)) {
|
||||
return true;
|
||||
}
|
||||
const stack = this.listeners[event.type].slice();
|
||||
|
|
|
@ -64,3 +64,26 @@ test(function removingNullEventListenerShouldSucceed() {
|
|||
assertEquals(document.removeEventListener("x", null, true), undefined);
|
||||
assertEquals(document.removeEventListener("x", null), undefined);
|
||||
});
|
||||
|
||||
test(function constructedEventTargetUseObjectPrototype() {
|
||||
const target = new EventTarget();
|
||||
const event = new Event("toString", { bubbles: true, cancelable: false });
|
||||
let callCount = 0;
|
||||
|
||||
function listener(e): void {
|
||||
assertEquals(e, event);
|
||||
++callCount;
|
||||
}
|
||||
|
||||
target.addEventListener("toString", listener);
|
||||
|
||||
target.dispatchEvent(event);
|
||||
assertEquals(callCount, 1);
|
||||
|
||||
target.dispatchEvent(event);
|
||||
assertEquals(callCount, 2);
|
||||
|
||||
target.removeEventListener("toString", listener);
|
||||
target.dispatchEvent(event);
|
||||
assertEquals(callCount, 2);
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue