-
Notifications
You must be signed in to change notification settings - Fork 59
Кулешова Ирина #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Кулешова Ирина #53
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,41 +4,90 @@ | |
| * Сделано задание на звездочку | ||
| * Реализованы методы several и through | ||
| */ | ||
| getEmitter.isStar = true; | ||
| getEmitter.isStar = false; | ||
| module.exports = getEmitter; | ||
|
|
||
|
|
||
| function getAllEvents(event) { | ||
| var splitted = event.split('.'); | ||
| var events = [splitted[0]]; | ||
| for (var i = 1; i < splitted.length; i++) { | ||
| events.push([events[i - 1], splitted[i]].join('.')); | ||
| } | ||
|
|
||
| return events; | ||
| } | ||
|
|
||
| /** | ||
| * Возвращает новый emitter | ||
| * @returns {Object} | ||
| */ | ||
| function getEmitter() { | ||
| return { | ||
| _events: [], | ||
|
|
||
| /** | ||
| * Подписаться на событие | ||
| * @param {String} event | ||
| * @param {Object} context | ||
| * @param {Function} handler | ||
| * @returns {Object} | ||
| */ | ||
| on: function (event, context, handler) { | ||
| console.info(event, context, handler); | ||
| if (!this._events.hasOwnProperty(event)) { | ||
| this._events[event] = []; | ||
| } | ||
| this._events[event].push({ | ||
| context: context, | ||
| handler: handler.bind(context) | ||
| }); | ||
|
|
||
| return this; | ||
| }, | ||
|
|
||
| /** | ||
| * Отписаться от события | ||
| * @param {String} event | ||
| * @param {Object} context | ||
| * @returns {Object} | ||
| */ | ||
| off: function (event, context) { | ||
| console.info(event, context); | ||
| var eventsToOff = Object.keys(this._events).filter( | ||
| function (key) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. давай переименуем There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. и еще эту строку можно к предыдущей приписать, так обычно пишут |
||
| return key === event || | ||
| (key.length > event.length && | ||
| key.substr(0, event.length + 1) === event + '.'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. вроде из условий (key.length > event.length && key.substr(0, event.length + 1) === event + '.')достаточно написать последнее |
||
| } | ||
| ); | ||
| for (var i = 0; i < eventsToOff.length; i++) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. почему бы не использовать |
||
| this._events[eventsToOff[i]] = this._events[eventsToOff[i]].filter( | ||
| function (item) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. тут |
||
| return item.context !== context; | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| return this; | ||
| }, | ||
|
|
||
| /** | ||
| * Уведомить о событии | ||
| * @param {String} event | ||
| * @returns {Object} | ||
| */ | ||
| emit: function (event) { | ||
| console.info(event); | ||
| var events = getAllEvents(event).reverse(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. если ты сплитишь event, а потом реверсишь, то почему бы сразу не пойти с конца? |
||
| for (var i = 0; i < events.length; i++) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. и тут вроде |
||
| var currEvent = events[i]; | ||
| if (!this._events[currEvent]) { | ||
| continue; | ||
| } | ||
| this._events[currEvent].forEach(function (item) { | ||
| item.handler(); | ||
| }); | ||
| } | ||
|
|
||
| return this; | ||
| }, | ||
|
|
||
| /** | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Название не отражает, что лежит в переменной