"이벤트 이미 터"란 무엇입니까?
http://microjs.com을 검색하면 "이벤트 이미 터"라는 레이블이 붙은 많은 라이브러리를 볼 수 있습니다. 나는 내가 자바 스크립트 언어의 기초에 대해 잘 알고 있다고 생각하지만, "이벤트 이미 터"가 무엇인지 실제로는 전혀 모른다.
누구든지 나를 깨달을 수 있습니까? 흥미롭게 들리 네요 ...
누구나들을 수 있는 이벤트 를 트리거합니다 . 다른 라이브러리는 다른 구현과 다른 목적을 제공하지만 기본 아이디어는 이벤트를 발행하고 이벤트를 구독하기위한 프레임 워크를 제공하는 것입니다.
jQuery의 예 :
// Subscribe to event.
$('#foo').bind('click', function() {
alert("Click!");
});
// Emit event.
$('#foo').trigger('click');
그러나 jQuery를 사용하여 이벤트를 생성하려면 DOM 객체가 있어야하며 임의의 객체에서 이벤트를 생성 할 수 없습니다. 여기서 이벤트 이미 터가 유용합니다.
다음은 커스텀 이벤트를 데모하는 의사 코드입니다 (위와 똑같은 패턴).
// Create custom object which "inherits" from emitter. Keyword "extend" is just a pseudo-code.
var myCustomObject = {};
extend(myCustomObject , EventEmitter);
// Subscribe to event.
myCustomObject.on("somethingHappened", function() {
alert("something happened!");
});
// Emit event.
myCustomObject.emit("somethingHappened");
node.js에서 이벤트는 해당 콜백이있는 문자열로 간단하게 설명 할 수 있습니다. 이벤트는 여러 번 "방출"될 수 있습니다 (즉, 해당 콜백이 호출 됨). 또는 처음 방출 될 때만 수신하도록 선택할 수 있습니다.
예:-
var example_emitter = new (require('events').EventEmitter);
example_emitter.on("test", function () { console.log("test"); });
example_emitter.on("print", function (message) { console.log(message); });
example_emitter.emit("test");
example_emitter.emit("print", "message");
example_emitter.emit("unhandled");
> var example_emitter = new (require('events').EventEmitter);
{}
> example_emitter.on("test", function () { console.log("test"); });
{ _events: { test: [Function] } }
> example_emitter.on("print", function (message) { console.log(message); });
{ _events: { test: [Function], print: [Function] } }
> example_emitter.emit("test");
test //console.log'd
true //return value
> example_emitter.emit("print", "message");
message //console.log'd
true //return value
> example_emitter.emit("unhandled");
false //return value
이것은의 모든 기본 기능을 보여줍니다 EventEmitter
. on or addListener
방법 (기본적으로 가입 방법은) 당신이 볼 수있는 이벤트 및 호출 할 콜백을 선택할 수 있습니다. 반면에 emit
메서드 (Publish 메서드)를 사용하면 이벤트를 "방출"할 수 있으며, 이벤트에 등록 된 모든 콜백이 '실행'(호출)됩니다.
소스에서 이벤트 이미 터 란 무엇입니까?
node.js의 간단한 예 :
var EventEmitter = require('events').EventEmitter;
var concert = new EventEmitter;
var singer = 'Coldplay';
concert.on('start', function (singer) {
console.log(`OMG ${singer}!`);
});
concert.on('finish', function () {
console.log(`It was the best concert in my life...`);
});
concert.emit('start', singer);
concert.emit('finish');
콜백 기능을 고려하십시오.
function test(int a, function(){
console.log("I am call-back function");
}){
console.log("I am a parent function");
}
Now, whenever the parent function is called on an event(a button click or any connection etc) , it first executes its code, and then control is passed to the call-back function. Now, an event emitter is an object/method which triggers an event as soon as some action takes place so as to pass the cntrol to the parent function. For-example- Server is an event emitter in Node.Js programming. It emits event of error as soon as the server encounters an error which passes the control to error parent function. Server emits an event of connection as soon as a socket gets connected to server, this event then triggers the parent function of getConnections, which indeed also takes a call-back function as argument. So, it indeed is a chain, which is triggered as something happens by event emitter which emits an event to start a function running.
참고URL : https://stackoverflow.com/questions/13438924/what-is-an-event-emitter
'Development Tip' 카테고리의 다른 글
.vimrc에서`set nocompatible`은 완전히 쓸모가 없습니까? (0) | 2020.11.05 |
---|---|
github 프로젝트에서 바이너리를 배포하는 가장 좋은 방법은 무엇입니까? (0) | 2020.11.05 |
PHP의 연결 풀링 (0) | 2020.11.05 |
ListPreference : 항목 값으로 문자열 배열을 사용하고 항목 값이 작동하지 않으므로 정수 배열을 사용합니다. (0) | 2020.11.05 |
JavaScript 코드에서 Python 함수 호출 (0) | 2020.11.05 |