You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: book.html
+8-8Lines changed: 8 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -491,11 +491,11 @@ <h3 id="errno">errno</h3>
491
491
</div>
492
492
<divclass='mp'>
493
493
<h1>Events</h1>
494
-
<p> The concept of an "event" is crucial to node, and used greatly throughout core and 3rd-party modules. Node's core module <em>events</em> supplies us with a single constructor, <em>EventEmitter</em>.</p>
494
+
<p> The concept of an "event" is crucial to node, and is used heavily throughout core and 3rd-party modules. Node's core module <em>events</em> supplies us with a single constructor, <em>EventEmitter</em>.</p>
495
495
496
496
<h2id="Emitting-Events">Emitting Events</h2>
497
497
498
-
<p>Typically an object inherits from <em>EventEmitter</em>, however our small example below illustrates the api. First we create an <code>emitter</code>, after which we can define any number of callbacks using the <code>emitter.on()</code> method which accepts the <em>name</em> of the event, and arbitrary objects passed as data. When <code>emitter.emit()</code> is called we are only required to pass the event <em>name</em>, followed by any number of arguments, in this case the <code>first</code> and <code>last</code> name strings.</p>
498
+
<p>Typically an object inherits from <em>EventEmitter</em>, however our small example below illustrates the API. First we create an <code>emitter</code>, after which we can define any number of callbacks using the <code>emitter.on()</code> method, which accepts the <em>name</em> of the event and arbitrary objects passed as data. When <code>emitter.emit()</code> is called, we are only required to pass the event <em>name</em>, followed by any number of arguments (in this case the <code>first</code> and <code>last</code> name strings).</p>
<h2id="Inheriting-From-EventEmitter">Inheriting From EventEmitter</h2>
513
513
514
-
<p>A perhaps more practical use of <code>EventEmitter</code>, and commonly used throughout node is to inherit from it. This means we can leave <code>EventEmitter</code>'s prototype untouched, while utilizing its api for our own means of world domination!</p>
514
+
<p>A more practical and common use of <code>EventEmitter</code>is to inherit from it. This means we can leave <code>EventEmitter</code>'s prototype untouched while utilizing its API for our own means of world domination!</p>
515
515
516
-
<p>To do so we begin by defining the <code>Dog</code> constructor, which of course will bark from time to time, also known as an <em>event</em>.</p>
516
+
<p>To do so, we begin by defining the <code>Dog</code> constructor, which of course will bark from time to time (also known as an <em>event</em>).</p>
@@ -522,12 +522,12 @@ <h2 id="Inheriting-From-EventEmitter">Inheriting From EventEmitter</h2>
522
522
}
523
523
</code></pre>
524
524
525
-
<p>Here we inherit from <code>EventEmitter</code>, so that we may use the methods provided such as <code>EventEmitter#on()</code> and <code>EventEmitter#emit()</code>. If the <code>__proto__</code> property is throwing you off, no worries! we will be touching on this later.</p>
525
+
<p>Here we inherit from <code>EventEmitter</code> so we can use the methods it provides, such as <code>EventEmitter#on()</code> and <code>EventEmitter#emit()</code>. If the <code>__proto__</code> property is throwing you off, don't worry, we'll be coming back to this later.</p>
<p>Now that we have our <code>Dog</code> set up, we can create.... simon! When simon barks we can let <em>stdout</em> know by calling <code>console.log()</code> within the callback. The callback it-self is called in context to the object, aka <code>this</code>.</p>
530
+
<p>Now that we have our <code>Dog</code> set up, we can create... Simon! When Simon barks, we can let <em>stdout</em> know by calling <code>console.log()</code> within the callback. The callback itself is called in the context of the object (aka <code>this</code>).</p>
531
531
532
532
<pre><code>var simon = new Dog('simon');
533
533
@@ -536,7 +536,7 @@ <h2 id="Inheriting-From-EventEmitter">Inheriting From EventEmitter</h2>
536
536
});
537
537
</code></pre>
538
538
539
-
<p>Bark twice a second:</p>
539
+
<p>Bark twice per second:</p>
540
540
541
541
<pre><code>setInterval(function(){
542
542
simon.emit('bark');
@@ -545,7 +545,7 @@ <h2 id="Inheriting-From-EventEmitter">Inheriting From EventEmitter</h2>
<p>As we have seen event listeners are simply functions which are called when we <code>emit()</code> an event. Although not seen often we can remove these listeners by calling the <code>removeListener(type, callback)</code> method. In the example below we emit the <em>message</em> "foo bar" every <code>300</code> milliseconds, which has the callback of <code>console.log()</code>. After 1000 milliseconds we call <code>removeListener()</code> with the same arguments that we passed to <code>on()</code> originally. To compliment this method is<code>removeAllListeners(type)</code> which removes all listeners associated to the given <em>type</em>.</p>
548
+
<p>As we have seen, event listeners are simply functions which are called when we <code>emit()</code> an event. We can remove these listeners by calling the <code>removeListener(type, callback)</code> method, although this isn't seen often. In the example below we emit the <em>message</em> "foo bar" every <code>300</code> milliseconds, which has a callback of <code>console.log()</code>. After 1000 milliseconds, we call <code>removeListener()</code> with the same arguments that we passed to <code>on()</code> originally. We could also have used<code>removeAllListeners(type)</code>, which removes all listeners registered to the given <em>type</em>.</p>
Copy file name to clipboardExpand all lines: chapters/events.html
+8-8Lines changed: 8 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,10 @@
1
1
<divclass='mp'>
2
2
<h1>Events</h1>
3
-
<p> The concept of an "event" is crucial to node, and used greatly throughout core and 3rd-party modules. Node's core module <em>events</em> supplies us with a single constructor, <em>EventEmitter</em>.</p>
3
+
<p> The concept of an "event" is crucial to node, and is used heavily throughout core and 3rd-party modules. Node's core module <em>events</em> supplies us with a single constructor, <em>EventEmitter</em>.</p>
4
4
5
5
<h2id="Emitting-Events">Emitting Events</h2>
6
6
7
-
<p>Typically an object inherits from <em>EventEmitter</em>, however our small example below illustrates the api. First we create an <code>emitter</code>, after which we can define any number of callbacks using the <code>emitter.on()</code> method which accepts the <em>name</em> of the event, and arbitrary objects passed as data. When <code>emitter.emit()</code> is called we are only required to pass the event <em>name</em>, followed by any number of arguments, in this case the <code>first</code> and <code>last</code> name strings.</p>
7
+
<p>Typically an object inherits from <em>EventEmitter</em>, however our small example below illustrates the API. First we create an <code>emitter</code>, after which we can define any number of callbacks using the <code>emitter.on()</code> method, which accepts the <em>name</em> of the event and arbitrary objects passed as data. When <code>emitter.emit()</code> is called, we are only required to pass the event <em>name</em>, followed by any number of arguments (in this case the <code>first</code> and <code>last</code> name strings).</p>
<h2id="Inheriting-From-EventEmitter">Inheriting From EventEmitter</h2>
22
22
23
-
<p>A perhaps more practical use of <code>EventEmitter</code>, and commonly used throughout node is to inherit from it. This means we can leave <code>EventEmitter</code>'s prototype untouched, while utilizing its api for our own means of world domination!</p>
23
+
<p>A more practical and common use of <code>EventEmitter</code>is to inherit from it. This means we can leave <code>EventEmitter</code>'s prototype untouched while utilizing its API for our own means of world domination!</p>
24
24
25
-
<p>To do so we begin by defining the <code>Dog</code> constructor, which of course will bark from time to time, also known as an <em>event</em>.</p>
25
+
<p>To do so, we begin by defining the <code>Dog</code> constructor, which of course will bark from time to time (also known as an <em>event</em>).</p>
@@ -31,12 +31,12 @@ <h2 id="Inheriting-From-EventEmitter">Inheriting From EventEmitter</h2>
31
31
}
32
32
</code></pre>
33
33
34
-
<p>Here we inherit from <code>EventEmitter</code>, so that we may use the methods provided such as <code>EventEmitter#on()</code> and <code>EventEmitter#emit()</code>. If the <code>__proto__</code> property is throwing you off, no worries! we will be touching on this later.</p>
34
+
<p>Here we inherit from <code>EventEmitter</code> so we can use the methods it provides, such as <code>EventEmitter#on()</code> and <code>EventEmitter#emit()</code>. If the <code>__proto__</code> property is throwing you off, don't worry, we'll be coming back to this later.</p>
<p>Now that we have our <code>Dog</code> set up, we can create.... simon! When simon barks we can let <em>stdout</em> know by calling <code>console.log()</code> within the callback. The callback it-self is called in context to the object, aka <code>this</code>.</p>
39
+
<p>Now that we have our <code>Dog</code> set up, we can create... Simon! When Simon barks, we can let <em>stdout</em> know by calling <code>console.log()</code> within the callback. The callback itself is called in the context of the object (aka <code>this</code>).</p>
40
40
41
41
<pre><code>var simon = new Dog('simon');
42
42
@@ -45,7 +45,7 @@ <h2 id="Inheriting-From-EventEmitter">Inheriting From EventEmitter</h2>
45
45
});
46
46
</code></pre>
47
47
48
-
<p>Bark twice a second:</p>
48
+
<p>Bark twice per second:</p>
49
49
50
50
<pre><code>setInterval(function(){
51
51
simon.emit('bark');
@@ -54,7 +54,7 @@ <h2 id="Inheriting-From-EventEmitter">Inheriting From EventEmitter</h2>
<p>As we have seen event listeners are simply functions which are called when we <code>emit()</code> an event. Although not seen often we can remove these listeners by calling the <code>removeListener(type, callback)</code> method. In the example below we emit the <em>message</em> "foo bar" every <code>300</code> milliseconds, which has the callback of <code>console.log()</code>. After 1000 milliseconds we call <code>removeListener()</code> with the same arguments that we passed to <code>on()</code> originally. To compliment this method is<code>removeAllListeners(type)</code> which removes all listeners associated to the given <em>type</em>.</p>
57
+
<p>As we have seen, event listeners are simply functions which are called when we <code>emit()</code> an event. We can remove these listeners by calling the <code>removeListener(type, callback)</code> method, although this isn't seen often. In the example below we emit the <em>message</em> "foo bar" every <code>300</code> milliseconds, which has a callback of <code>console.log()</code>. After 1000 milliseconds, we call <code>removeListener()</code> with the same arguments that we passed to <code>on()</code> originally. We could also have used<code>removeAllListeners(type)</code>, which removes all listeners registered to the given <em>type</em>.</p>
Copy file name to clipboardExpand all lines: chapters/events.md
+8-8Lines changed: 8 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,11 @@
1
1
2
2
# Events
3
3
4
-
The concept of an "event" is crucial to node, and used greatly throughout core and 3rd-party modules. Node's core module _events_ supplies us with a single constructor, _EventEmitter_.
4
+
The concept of an "event" is crucial to node, and is used heavily throughout core and 3rd-party modules. Node's core module _events_ supplies us with a single constructor, _EventEmitter_.
5
5
6
6
## Emitting Events
7
7
8
-
Typically an object inherits from _EventEmitter_, however our small example below illustrates the api. First we create an `emitter`, after which we can define any number of callbacks using the `emitter.on()` method which accepts the _name_ of the event, and arbitrary objects passed as data. When `emitter.emit()` is called we are only required to pass the event _name_, followed by any number of arguments, in this case the `first` and `last` name strings.
8
+
Typically an object inherits from _EventEmitter_, however our small example below illustrates the API. First we create an `emitter`, after which we can define any number of callbacks using the `emitter.on()` method, which accepts the _name_ of the event and arbitrary objects passed as data. When `emitter.emit()` is called, we are only required to pass the event _name_, followed by any number of arguments (in this case the `first` and `last` name strings).
9
9
10
10
var EventEmitter = require('events').EventEmitter;
11
11
@@ -20,37 +20,37 @@ Typically an object inherits from _EventEmitter_, however our small example belo
20
20
21
21
## Inheriting From EventEmitter
22
22
23
-
A perhaps more practical use of `EventEmitter`, and commonly used throughout node is to inherit from it. This means we can leave `EventEmitter`'s prototype untouched, while utilizing its api for our own means of world domination!
23
+
A more practical and common use of `EventEmitter`is to inherit from it. This means we can leave `EventEmitter`'s prototype untouched while utilizing its API for our own means of world domination!
24
24
25
-
To do so we begin by defining the `Dog` constructor, which of course will bark from time to time, also known as an _event_.
25
+
To do so, we begin by defining the `Dog` constructor, which of course will bark from time to time (also known as an _event_).
26
26
27
27
var EventEmitter = require('events').EventEmitter;
28
28
29
29
function Dog(name) {
30
30
this.name = name;
31
31
}
32
32
33
-
Here we inherit from `EventEmitter`, so that we may use the methods provided such as `EventEmitter#on()` and `EventEmitter#emit()`. If the `__proto__` property is throwing you off, no worries! we will be touching on this later.
33
+
Here we inherit from `EventEmitter` so we can use the methods it provides, such as `EventEmitter#on()` and `EventEmitter#emit()`. If the `__proto__` property is throwing you off, don't worry, we'll be coming back to this later.
34
34
35
35
Dog.prototype.__proto__ = EventEmitter.prototype;
36
36
37
-
Now that we have our `Dog` set up, we can create.... simon! When simon barks we can let _stdout_ know by calling `console.log()` within the callback. The callback it-self is called in context to the object, aka `this`.
37
+
Now that we have our `Dog` set up, we can create... Simon! When Simon barks, we can let _stdout_ know by calling `console.log()` within the callback. The callback itself is called in the context of the object (aka `this`).
38
38
39
39
var simon = new Dog('simon');
40
40
41
41
simon.on('bark', function(){
42
42
console.log(this.name + ' barked');
43
43
});
44
44
45
-
Bark twice a second:
45
+
Bark twice per second:
46
46
47
47
setInterval(function(){
48
48
simon.emit('bark');
49
49
}, 500);
50
50
51
51
## Removing Event Listeners
52
52
53
-
As we have seen event listeners are simply functions which are called when we `emit()` an event. Although not seen often we can remove these listeners by calling the `removeListener(type, callback)` method. In the example below we emit the _message_ "foo bar" every `300` milliseconds, which has the callback of `console.log()`. After 1000 milliseconds we call `removeListener()` with the same arguments that we passed to `on()` originally. To compliment this method is`removeAllListeners(type)` which removes all listeners associated to the given _type_.
53
+
As we have seen, event listeners are simply functions which are called when we `emit()` an event. We can remove these listeners by calling the `removeListener(type, callback)` method, although this isn't seen often. In the example below we emit the _message_ "foo bar" every `300` milliseconds, which has a callback of `console.log()`. After 1000 milliseconds, we call `removeListener()` with the same arguments that we passed to `on()` originally. We could also have used`removeAllListeners(type)`, which removes all listeners registered to the given _type_.
54
54
55
55
var EventEmitter = require('events').EventEmitter;
0 commit comments