Node.js Tutorial - 22 - Extending from EventEmitter
Summary
TLDRThis video tutorial introduces how to build a custom module in Node.js that extends the EventEmitter class. The presenter demonstrates creating a 'pizzashop.js' file with a class that handles order placements and displays the order number. By inheriting from EventEmitter, the PizzaShop class can emit custom events, which are then listened to and handled in 'index.js'. The video also shows how to integrate the event-driven architecture with other modules, such as a 'drinkmachine.js', to create a loosely coupled system. The presenter encourages viewers to learn more about class inheritance in JavaScript and hints at upcoming videos on built-in modules like FS, streams, and HTTP.
Takeaways
- 📚 The video introduces creating a custom module in Node.js that builds on the Event Emitter class.
- 🍕 A 'pizzashop.js' file is created to define a PizzaShop class with a constructor and methods for order handling.
- 🔢 The PizzaShop class initializes an order number to zero and includes methods to increment and display the order number.
- 📦 The PizzaShop class is exported for use in other modules, demonstrating modularity in Node.js applications.
- 📝 In 'index.js', the PizzaShop class is imported and an instance is created to test the order methods.
- 🔄 The video explains how to use event-driven architecture in the PizzaShop class by utilizing inheritance from the Event Emitter class.
- 📜 The 'extends' keyword is used to inherit from the Event Emitter, allowing the PizzaShop class to emit events.
- 📡 The 'this.emit' method is used within the order method to emit an 'order' event with 'size' and 'Topping' parameters.
- 📌 Event listeners are attached in 'index.js' to handle the 'order' event and log the received order details to the console.
- 🥤 A separate module, 'drinkmachine.js', is introduced to handle drink serving logic based on the order size.
- 🔗 The DrinkMachine class is used within the order event listener to serve a complimentary drink for large orders, showcasing event-based interaction between modules.
Q & A
What is the main focus of the video?
-The video focuses on demonstrating how to create a custom module in Node.js that builds on top of the event emitter class, using the example of a pizza shop.
What is the purpose of creating a 'pizzashop.js' file?
-The 'pizzashop.js' file is created to define a PizzaShop class that has its own properties and methods for handling orders in an event-driven architecture.
What are the two methods defined in the PizzaShop class?
-The two methods defined in the PizzaShop class are one to place an order, which increments the order number, and another called displayOrderNumber to view the current order number.
How is the PizzaShop class exported for use in other modules?
-The PizzaShop class is exported using the `module.exports` syntax, allowing it to be imported and used in other modules like 'index.js'.
What is the purpose of the 'extends' keyword in JavaScript?
-The 'extends' keyword in JavaScript is used for inheritance, allowing one class to inherit the properties and methods of another class.
How does the PizzaShop class inherit from the event emitter class?
-The PizzaShop class inherits from the event emitter class by using the `extends` keyword and invoking the `super` function within its constructor.
What is the significance of emitting an event in the order method?
-Emitting an event in the order method allows the PizzaShop class to trigger custom events, such as 'order', and pass parameters like size and toppings to event listeners.
What is the role of the 'DrinkMachine' class in the script?
-The 'DrinkMachine' class is a separate module that handles the logic for serving drinks based on the order size, and it is used to demonstrate how different modules can interact using events.
How does the 'DrinkMachine' class interact with the PizzaShop class?
-The 'DrinkMachine' class interacts with the PizzaShop class by being called within the order event listener, where it receives the order size and decides whether to serve a complimentary drink.
What is the advantage of using event-driven architecture in this scenario?
-The advantage of using event-driven architecture is that it allows different modules to communicate and react to events without being tightly coupled, promoting modularity and flexibility in the code.
Why is it important to understand that built-in modules like FS, streams, and HTTP extend from the event emitter class?
-Understanding that built-in modules extend from the event emitter class is important because it highlights the versatility and power of the event-driven architecture in Node.js, allowing developers to handle asynchronous events in a standardized way.
Outlines
🍕 Building a Custom Module with Event Emitter
This paragraph introduces the concept of creating a custom module that extends the functionality of the event emitter class. The script describes the process of setting up a 'pizzashop.js' file where a 'PizzaShop' class is defined. This class includes a constructor to initialize an order number and two methods: one for placing an order that increments the order number, and another to display the current order number. The class is then exported for use in other modules. In 'index.js', the existing code is commented out, the 'PizzaShop' class is imported, an instance is created, and methods are invoked to demonstrate the class's properties and methods. The paragraph also discusses the use of event-driven architecture in the pizza shop class through inheritance, where the 'PizzaShop' class inherits from the 'EventEmitter' class, allowing it to emit custom events.
📡 Event-Driven Architecture with Modules
The second paragraph delves into the implementation of an event-driven architecture using the 'PizzaShop' class. It explains how to emit an event within the 'order' method of the 'PizzaShop' class, which now includes parameters for size and toppings. The paragraph then shows how to attach listeners to these events in 'index.js', where the size and toppings are logged to the console. Additionally, it introduces the concept of separating logic into different modules, exemplified by creating a 'drinkmachine.js' file with a 'DrinkMachine' class that has a 'serveDrink' method. This method logs a message to the console if the order size is large. The 'DrinkMachine' class is then used within the same event listener in 'index.js' to demonstrate the modular and decoupled approach to handling events. The paragraph concludes by emphasizing the importance of understanding how modules like 'PizzaShop' can extend from 'EventEmitter' to emit and react to custom events, a concept that is fundamental to many built-in Node.js modules.
Mindmap
Keywords
💡Event Emitter
💡Module
💡Constructor
💡Inheritance
💡Class
💡Method
💡Event-Driven Architecture
💡Custom Events
💡Listeners
💡ES2015 (ECMAScript 2015)
💡Built-in Modules
Highlights
Introduction to creating a custom module that builds on top of the event emitter class.
Creating a new file 'pizzashop.js' and defining a 'PizzaShop' class with a constructor and methods.
Initializing the order number to zero and adding methods to place an order and display the current order number.
Exporting the 'PizzaShop' class for use in other modules.
Importing and instantiating the 'PizzaShop' class in 'index.js'.
Demonstrating the pizza shop's ability to handle orders using an event-driven architecture with the 'events' module.
Using inheritance in JavaScript to extend one class and inherit the functionality of another, specifically the 'PizzaShop' class inheriting from the 'event emitter' class.
Modifying the 'PizzaShop' class to emit an event with parameters such as size and topping.
Attaching event listeners in 'index.js' to handle the 'order' event and log the received size and toppings.
Creating a separate module 'drinkmachine.js' for the drink serving logic.
Introducing the 'DrinkMachine' class with a method to serve drinks based on the order size.
Exporting and using the 'DrinkMachine' class in 'index.js' to serve drinks when an order is placed.
Tying together different modules without tightly coupling them using events.
Highlighting that modules like 'pizzashop' can extend from 'event emitter', allowing them to emit and react to custom events.
Mentioning that most built-in modules in Node.js, such as FS, streams, and HTTP, also extend from the 'event emitter' class.
Encouraging viewers to remember the importance of extending from 'event emitter' for built-in modules in upcoming videos.
Invitation to subscribe to the channel for more educational content on Node.js and related topics.
Transcripts
in the previous video we learned about
the events module which Returns the
event emitter class
using an instance of event emitter we
were able to emit events and respond to
the emitted events
in this video I want to show you how we
can create our own module that builds on
top of the event emitter class
let's begin
I'm going to create a new file called
pizzashop.js
within the file I'm going to define a
pizza shop class
pizza shop
has a Constructor
where we initialize or the number to
zero
and we're going to add two methods
one to place an order
where we increment order number
and another called display order number
to view the current order number
let's export the class for use in other
modules
in index.js
let's comment out the existing code
and import the class
Khan's pizza shop is equal to require
from dot slash pizza shop
in the next line
let's create an instance
pizza shop is equal to new pizza shop
we can then invoke
pizzashop.order
followed by pizza shop dot display order
number
if we run node index in the terminal
we should see the message current order
number 1.
so we have a pizza shop that has its own
properties and methods
however we would like the shop to be
able to handle orders using the
event-driven architecture
that is using the events module
now the solution for that is inheritance
in JavaScript we can extend one class to
inherit the functionality of another
class
in our case pizza shop class is going to
inherit from the event emitter class
and here's how we do that
Begin by importing the event emitter
class
const event emitter is equal to require
node colon events
next use the extents keyword with the
pizza shop class
pizza shop extends event emitter
finally within the Constructor invoke
super
this class-based inheritance is a
feature introduced in es2015 please do
watch my Advanced JavaScript crash
course if you would like to learn more
about what the extents and super
keywords do under the hood
but what this inheritance allows us to
do is use the pizza shop class as if it
is an event emitter class
so in the order method
we can now emit an event
but this time we don't have a separate
emitter object
Instead This keyword refers to the
emitted object
so this dot emit
the event name is order and let's add
size and Topping as parameters
with an event now being emitted we can
go back to index.js and attach listeners
pizza shop dot on the event name is
order
The Listener receives size and Topping
which we're going to log to the console
while placing the order let's specify
the size and the toppings
if we now rerun node index
we see the expected log statement
are they received making a large pizza
with mushrooms
we also see the current order number is
1.
let's now register the other event
listener which has to do with serving a
drink
now what I will do is extract out the
drink logic into a separate module
so new file
called drinkmachine.js
and within the file
let's create a new class
Drink Machine
one method called serve drink
which accepts size
if the order size is large
we log to the console serving
complementary drink
let's export it for use in index.js
back in index
const Drink Machine is equal to require
dot slash Drink Machine
and create a new instance
next within the same order event
listener
call Drink Machine dot served drink
pass in size
rerun node index
and we see the expected log statements
order received making a large pizza with
mushrooms serving complimentary drink
current order number one
as you can see using events we are able
to tie together different modules
without having to tightly couple them
but what I want you to take away from
this video is the fact that modules such
as pizza shop can extend from event
emitter
allowing them to Emit and react to their
own custom events
and the reason I want you to remember
this is because most of the built-in
modules especially FS streams and HTTP
which we are about to learn in the
upcoming videos also extend from the
event emitter class
all right with this in mind let's
proceed to the remaining built-in
modules in node.js
thank you for watching please do
consider subscribing to the channel I'll
see you in the next one
関連動画をさらに表示
Webflow Unlocks Class Attribute
2 Simple Ways To Code Linked Lists In Python
Modern Graphical User Interfaces in Python
First-Class and Higher-Order Functions | JavaScript 🔥 | Lecture 121
Automatic number plate recognition (ANPR) with Yolov9 and EasyOCR
Operator Overloading in C++ Programming | C++ Programming for Beginners
5.0 / 5 (0 votes)