From 2d945fd64581c279e22e6b020714a703d7fbd248 Mon Sep 17 00:00:00 2001 From: daGo Date: Sat, 5 May 2018 10:53:43 +0300 Subject: [PATCH] doc: clarify `this` in event listeners this === eventEmitter or this === instance of EventEmitter, but it's this is not EventEmitter. PR-URL: https://github.com/nodejs/node/pull/20537 Reviewed-By: Anna Henningsen Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Trivikram Kamat Reviewed-By: Tiancheng "Timothy" Gu Reviewed-By: Vse Mozhet Byt --- doc/api/events.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/doc/api/events.md b/doc/api/events.md index 8b3cb966afd274..0dd9e4be5dbc6d 100644 --- a/doc/api/events.md +++ b/doc/api/events.md @@ -43,21 +43,21 @@ myEmitter.emit('event'); ## Passing arguments and `this` to listeners The `eventEmitter.emit()` method allows an arbitrary set of arguments to be -passed to the listener functions. It is important to keep in mind that when an -ordinary listener function is called by the `EventEmitter`, the standard `this` -keyword is intentionally set to reference the `EventEmitter` to which the +passed to the listener functions. It is important to keep in mind that when +an ordinary listener function is called, the standard `this` keyword +is intentionally set to reference the `EventEmitter` instance to which the listener is attached. ```js const myEmitter = new MyEmitter(); myEmitter.on('event', function(a, b) { - console.log(a, b, this); + console.log(a, b, this, this === myEmitter); // Prints: // a b MyEmitter { // domain: null, // _events: { event: [Function] }, // _eventsCount: 1, - // _maxListeners: undefined } + // _maxListeners: undefined } true }); myEmitter.emit('event', 'a', 'b'); ```