繁体   English   中英

事件和函数的区别?

[英]Difference between Events and Functions?

我是 Node 新手,我很难理解事件和函数之间的主要区别。 两者都需要被触发,那么如果我们必须触发它,为什么我们还需要一个事件呢?

与触发 Function 有何不同?

示例代码:

var events = require('events');
var eventEmitter = new events.EventEmitter();

eventEmitter.on('event1', function () {
    console.log('Event 1 executed.');
    eventEmitter.emit('event2');
});

eventEmitter.on('event2', function() {
    console.log('Event 2 executed.');
});

eventEmitter.emit('event1');
console.log('Program Ended.');

我们可以通过函数来达到同样的效果,对吧?

我确信这在 Node 中具有非常重要的意义(否则它将不存在,哈哈),但我很难理解它。

帮助表示赞赏::)

事件处理异步操作。 从它们可以互换的意义上说,它们与功能并不真正相关。

eventEmitter.on本身就是一个函数,它接受两个参数作为事件名称,然后是一个在事件发生时要执行的函数(回调)。

eventEmitter.on(evt, callback)

没有办法告诉何时将发出事件,因此您提供了在事件发生时要执行的回调。

在您的示例中,您控制何时触发事件,这与现实世界使用不同,在现实世界中,您可能让服务器侦听可以随时连接的连接。

server.listen('9000', function(){
    console.log('Server started');
});

server.on('connection', function(client){
    console.log('New client connected');
    doSomethingWithClient(client);
});

//series of synchronous events
function doSomethingWithClient(client){
    //something with client
}

对于server.listen服务器不会立即启动,一旦准备好回调被调用

server.on('connection')监听客户端连接,它们可以随时到来。 然后在连接发生时触发该事件,从而导致运行回调。

然后是doSomethingWithClient这只是一个函数,当客户端连接发生时,它会执行一组同步操作。

在网络服务器代码中有用的事件(在端口上处于活动状态)不在普通脚本中,在普通脚本中,事件的行为将与函数相同,因为只要端口启动,事件就会持续活动并在端口上侦听请求,所以如果我们使用 function 代替, function 仅在运行 .js 文件时运行一次,因此函数无法捕获传入请求和响应。

Example: in below code if you see output of below function dummy_func() triggered immediately when js file is ran & printed statement ConditionReport: A client Connected: 0 only once as output, but the EventReport: A client Connected: printed only when opened http ://localhost:58080 在浏览器中,如果我打开相同的 http://localhost:58080 它再次在另一个选项卡中打印EventReport: A client Connected: 3

 const express = require('express'); const app = express(); const path = require('path'); const PORT = process.env.PORT || 58080; // Load all static html files in the directory, here index.html file open as default at http://localhost:58080/ but to load html like Resume.html we should call complete http://localhost:58080/Resume.html app.use(express.static(path.join(__dirname))); // Configure Port var server_object=app.listen(PORT, () => console.log("Server listening on port " + PORT)); //Possible incomming Events var incomming_events=server_object.eventNames() console.log("\nserver_object:",incomming_events); //On Event var i=0 server_object.on('connection',()=>{ i=i+1 console.log("\nEventReport: A client Connected:",i); }); //Using if condition instead of Event function dummy_func(j){ console.log("\nConditionReport: A client Connected:",j,"\n"); } var j=0 if (incomming_events.includes('connection')){ dummy_func(j) j=j+1 }

OUTPUT:

stataraju@statara-ltm7wjr Example2 % node index.js

server_object: [ 'request', 'connection', 'listening' ]

ConditionReport: A client Connected: 0 

Server listening on port 58080

EventReport: A client Connected: 1

EventReport: A client Connected: 2

EventReport: A client Connected: 3

事件是工具( .on().emit()等)中用于设置和执行回调的标识符。 函数是可重用的代码。

我想我看到的最大区别是事件发射器可以触发多个正在侦听的事件,而仅调用一个函数只会触发一件事。

因此,例如,您可能在游戏中有许多对象都在等待增加其动画的 step 事件。

尽管我更愿意只使用函数,但它们调试起来非常痛苦。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM