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

doc: Adding 'use strict' to ES6 examples #6380

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions doc/api/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ myEmitter.emit('event');

Any object can become an `EventEmitter` through inheritance. The example above
uses the traditional Node.js style prototypical inheritance using
the `util.inherits()` method. It is, however, possible to use ES6 classes as
well:
the `util.inherits()` method. It is, however, possible when using strict mode,
to use ES6 classes as well:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incorrect, ES6 classes work without strict mode now.


```js
'use strict';

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example works without use strict.

const EventEmitter = require('events');

class MyEmitter extends EventEmitter {}
Expand Down Expand Up @@ -86,6 +88,8 @@ It is possible to use ES6 Arrow Functions as listeners, however, when doing so,
the `this` keyword will no longer reference the `EventEmitter` instance:

```js
'use strict';

Copy link
Member

@ChALkeR ChALkeR Apr 27, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example both works without use strict and is partial (a continuation of the first example), that means that this is not the correct place for use strict.

const myEmitter = new MyEmitter();
myEmitter.on('event', (a, b) => {
console.log(a, b, this);
Expand All @@ -103,6 +107,8 @@ listener functions can switch to an asynchronous mode of operation using
the `setImmediate()` or `process.nextTick()` methods:

```js
'use strict';

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example both works without use strict and is partial (a continuation of the previous example), that means that this is not the correct place for use strict.

const myEmitter = new MyEmitter();
myEmitter.on('event', (a, b) => {
setImmediate(() => {
Expand All @@ -118,6 +124,8 @@ When a listener is registered using the `eventEmitter.on()` method, that
listener will be invoked _every time_ the named event is emitted.

```js
'use strict';

Copy link
Member

@ChALkeR ChALkeR Apr 27, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example both works without use strict and is partial (a continuation of the first example), that means that this is not the correct place for use strict.

const myEmitter = new MyEmitter();
var m = 0;
myEmitter.on('event', () => {
Expand All @@ -133,6 +141,8 @@ Using the `eventEmitter.once()` method, it is possible to register a listener
that is immediately unregistered after it is called.

```js
'use strict';

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example both works without use strict and is partial (a continuation of the first example), that means that this is not the correct place for use strict.

const myEmitter = new MyEmitter();
var m = 0;
myEmitter.once('event', () => {
Expand Down Expand Up @@ -180,6 +190,8 @@ As a best practice, developers should always register listeners for the
`'error'` event:

```js
'use strict';

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example both works without use strict and is partial (a continuation of the first example), that means that this is not the correct place for use strict.

const myEmitter = new MyEmitter();
myEmitter.on('error', (err) => {
console.log('whoops! there was an error');
Expand Down Expand Up @@ -216,6 +228,8 @@ but important side effect: any *additional* listeners registered to the same
listener that is in the process of being added.

```js
'use strict';

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example both works without use strict and is partial (a continuation of the first example), that means that this is not the correct place for use strict.

const myEmitter = new MyEmitter();
// Only do this once so we don't loop forever
myEmitter.once('newListener', (event, listener) => {
Expand Down Expand Up @@ -250,6 +264,8 @@ A class method that returns the number of listeners for the given `eventName`
registered on the given `emitter`.

```js
'use strict';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example both works without use strict and is partial (a continuation of the first example), that means that this is not the correct place for use strict.


const myEmitter = new MyEmitter();
myEmitter.on('event', () => {});
myEmitter.on('event', () => {});
Expand Down Expand Up @@ -302,6 +318,8 @@ Returns an array listing the events for which the emitter has registered
listeners. The values in the array will be strings or Symbols.

```js
'use strict';

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example doesn't work because of a previous mistype (I will file a PR to fix it).

After fixing the mistype, this example works without use strict.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #6417 for the mistype fix.

const EventEmitter = require('events');
const myEE = new EventEmitter();
myEE.on('foo', () => {});
Expand Down Expand Up @@ -362,6 +380,8 @@ By default, event listeners are invoked in the order they are added. The
event listener to the beginning of the listeners array.

```js
'use strict';

Copy link
Member

@ChALkeR ChALkeR Apr 27, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example both works without use strict and is partial (a continuation of a previous example), that means that this is not the correct place for use strict.

const myEE = new EventEmitter();
myEE.on('foo', () => console.log('a'));
myEE.prependListener('foo', () => console.log('b'));
Expand Down Expand Up @@ -393,6 +413,8 @@ By default, event listeners are invoked in the order they are added. The
event listener to the beginning of the listeners array.

```js
'use strict';

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example both works without use strict and is partial (a continuation of a previous example), that means that this is not the correct place for use strict.

const myEE = new EventEmitter();
myEE.once('foo', () => console.log('a'));
myEE.prependOnceListener('foo', () => console.log('b'));
Expand Down Expand Up @@ -474,6 +496,8 @@ finishes execution will not remove them from `emit()` in progress. Subsequent
events will behave as expected.

```js
'use strict';

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example both works without use strict and is partial (a continuation of the first example), that means that this is not the correct place for use strict.

const myEmitter = new MyEmitter();

var callbackA = () => {
Expand Down