什么是`emit` javascript 函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32246262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is `emit` javascript function?
提问by avasin
While looking through saxnodejs module, i saw multiple emit
function calls, but i can't find any information about it.
在查看saxnodejs 模块时,我看到了多个emit
函数调用,但找不到任何有关它的信息。
Is it some V8 native tool for emitting events? Why sax-js
do not use EventEmitter for streams then?
它是一些用于发出事件的 V8 原生工具吗?那么为什么sax-js
不将 EventEmitter 用于流呢?
采纳答案by brk
Please look at line number 624 of same file
function emit (parser, event, data) {
parser[event] && parser[event](data)
}
回答by Psylogic
In node.js an event can be described simply as a string with a corresponding callback. An event can be "emitted" (or in other words, the corresponding callback be called) multiple times or you can choose to only listen for the first time it is emitted.
在 node.js 中,事件可以简单地描述为带有相应回调的字符串。一个事件可以被多次“发出”(或者换句话说,相应的回调被调用)或者你可以选择只在第一次发出时监听。
The on or addListener method (basically the subscription method) allows you to choose the event to watch for and the callback to be called. The emit method (the publish method), on the other hand, allows you to "emit" an event, which causes all callbacks registered to the event to 'fire', (get called).
on 或 addListener 方法(基本上是订阅方法)允许您选择要监视的事件和要调用的回调。另一方面,emit 方法(发布方法)允许您“发出”一个事件,这会导致注册到该事件的所有回调“触发”(被调用)。
reference: https://docs.nodejitsu.com/articles/getting-started/control-flow/what-are-event-emitters/(This is an outdated link and doesn't work anymore)
参考:https: //docs.nodejitsu.com/articles/getting-started/control-flow/what-are-event-emitters/(这是一个过时的链接,不再有效)
回答by Hashmatullah Noorzai
Short: Emit job is to trigger named event(s) which in turn causes function objects also called listeners to be called.
简而言之:发射工作是触发命名事件,这反过来又会导致函数对象也被称为侦听器被调用。
Detailed: Node.js core API is built around an idiomatic asynchronousevent-driven architecture in which certain kinds of objects (called "emitters") periodically emit named events that cause Function objects ("listeners") to be called.
详细:Node.js 核心 API 是围绕惯用的异步事件驱动架构构建的,其中某些类型的对象(称为“发射器”)周期性地发出命名事件,导致函数对象(“侦听器”)被调用。
All objects that emit events are instances of the EventEmitter class. These objects expose an eventEmitter.on() function that allows one or more functions to be attached to named events emitted by the object.
所有发出事件的对象都是 EventEmitter 类的实例。这些对象公开了一个 eventEmitter.on() 函数,该函数允许将一个或多个函数附加到对象发出的命名事件。
When the EventEmitter object emits an event, all of the functions attached to that specific event are called synchronously. Any values returned by the called listeners are ignored and will be discarded.
当 EventEmitter 对象发出一个事件时,所有附加到该特定事件的函数都会被同步调用。被调用的侦听器返回的任何值都将被忽略并被丢弃。
Read More here
在这里阅读更多