Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Node.js event

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you the "sample Analysis of Node.js events", which is easy to understand and clear. I hope it can help you solve your doubts. Let the editor lead you to study and study the article "sample Analysis of Node.js events".

Quick overview

To access this module, simply use the following statement:

Require ('events')

Requires ('events'). EventEmitter

In particular, all objects in node that can trigger events are basically instances of the latter. Let's create a simple demo program, Dummy:

Dummy.js

/ / basic imports var events = require ('events'); / / for us to do a require later module.exports = Dummy; function Dummy () {events.EventEmitter.call (this);} / / inherit events.EventEmitter Dummy.super_ = events.EventEmitter; Dummy.prototype = Object.create (events.EventEmitter.prototype, {constructor: {value: Dummy, enumerable: false}})

The above code focuses on how to use EventEmitter to extend objects and inherit all prototype objects, methods. Wait.

Now, let's assume that Dummy has a cooking () method that triggers the 'cooked' event once the food is cooked and calls a callback function named' eat'.

Dummy-cooking.js

Dummy.prototype.cooking = function (chicken) {var self = this; self.chicken = chicken; self.cook = cook (); / / assume dummy function that'll do the cooking self.cook (chicken, function (cooked_chicken) {self.chicken = cooked_chicken; self.emit ('cooked', self.chicken);}); return self;}

Now, this module has been completed. We can use it in the main program.

Dummy-node.js

/ / A nonsensical node.js program var Dummy = require ('. / dummy'); var kenny = new Dummy (); var dinner = kenny.cooking (fried_chix); dinner.on ('cooked', function (chicken) {/ / eat up!} >

So basically, node.js executes the script, then waits for the 'cooked' event to be triggered, and after the event triggers, calls the callback function and passes the returned parameters.

Is there anything else to pay attention to?

It is worth noting that the "subclasses" and events used in the example have some extremes (a bit of an overkill). EventEmitter fires only one event (EventEmitter for things that only fire one event once) at a time. If you create only a few instances, you can add the method directly to the instance itself, and it may be better to use an asynchronous function if you want to trigger the underlying event.

About events.EventEmitter, you also need to pay attention to one special event: 'error'. This event is triggered when any error occurs, and when no listener listens for the event, node will throw an exception and end the application. (thank Tim for pointing this out.)

The above is all the content of the article "sample Analysis of Node.js events". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report