Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

events: set default handler value #41970

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/internal/event_target.js
Expand Up @@ -929,7 +929,7 @@ function defineEventHandler(emitter, name) {
// 8.1.5.1 Event handlers - basically `on[eventName]` attributes
ObjectDefineProperty(emitter, `on${name}`, {
get() {
return this[kHandlers]?.get(name)?.handler;
return this[kHandlers]?.get(name)?.handler ?? null;
},
set(value) {
if (!this[kHandlers]) {
Expand Down
11 changes: 9 additions & 2 deletions test/parallel/test-eventtarget.js
Expand Up @@ -408,6 +408,13 @@ let asyncTest = Promise.resolve();
target.onfoo = common.mustCall();
target.dispatchEvent(new Event('foo'));
}

{
const target = new EventTarget();
defineEventHandler(target, 'foo');
strictEqual(target.onfoo, null);
}

{
const target = new EventTarget();
defineEventHandler(target, 'foo');
Expand Down Expand Up @@ -623,14 +630,14 @@ let asyncTest = Promise.resolve();
strictEqual(et.constructor.name, 'EventTarget');
}
{
// Weak event handlers work
// Weak event listeners work
const et = new EventTarget();
const listener = common.mustCall();
et.addEventListener('foo', listener, { [kWeakHandler]: et });
et.dispatchEvent(new Event('foo'));
}
{
// Weak event handlers can be removed and weakness is not part of the key
// Weak event listeners can be removed and weakness is not part of the key
const et = new EventTarget();
const listener = common.mustNotCall();
et.addEventListener('foo', listener, { [kWeakHandler]: et });
Expand Down