-
Notifications
You must be signed in to change notification settings - Fork 59
Квашнина Ульяна #50
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?
Квашнина Ульяна #50
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,81 @@ | |
* Сделано задание на звездочку | ||
* Реализованы методы several и through | ||
*/ | ||
getEmitter.isStar = true; | ||
getEmitter.isStar = false; | ||
module.exports = getEmitter; | ||
|
||
/** | ||
* Возвращает новый emitter | ||
* @returns {Object} | ||
*/ | ||
function getEmitter() { | ||
var students = []; | ||
|
||
return { | ||
|
||
/** | ||
* Подписаться на событие | ||
* @param {String} event | ||
* @param {Object} context | ||
* @param {Function} handler | ||
* @returns {Object} | ||
*/ | ||
on: function (event, context, handler) { | ||
console.info(event, context, handler); | ||
students.push({ event: event, | ||
context: context, | ||
handler: handler } | ||
); | ||
|
||
return this; | ||
}, | ||
|
||
/** | ||
* Отписаться от события | ||
* @param {String} event | ||
* @param {Object} context | ||
* @returns {Object} | ||
*/ | ||
off: function (event, context) { | ||
console.info(event, context); | ||
if (typeof (event) === 'string' && typeof (context) === 'object') { | ||
students = students.filter(function (subscriber) { | ||
|
||
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 (subscriber.context !== context || subscriber.event !== event) && | ||
!(subscriber.event.indexOf(event + '.') === 0); | ||
}); | ||
} | ||
|
||
return this; | ||
}, | ||
|
||
/** | ||
* Уведомить о событии | ||
* @param {String} event | ||
* @returns {Object} | ||
*/ | ||
emit: function (event) { | ||
console.info(event); | ||
var emitEvent = []; | ||
var splitEvent = event.split('.'); | ||
var flag = 1; | ||
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. Непонятно из названия, для чего конкретно этот флаг. |
||
|
||
for (var i = 0; i < splitEvent.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 event_ = splitEvent[0]; | ||
for (var j = 1; j < flag; j++) { | ||
event_ += '.' + splitEvent[j]; | ||
} | ||
flag++; | ||
emitEvent.push(event_); | ||
} | ||
emitEvent.reverse(); | ||
|
||
emitEvent.forEach(function (eventt) { | ||
students.forEach(function (subscriber) { | ||
if (subscriber.event === eventt) { | ||
subscriber.handler.call(subscriber.context); | ||
} | ||
}); | ||
}); | ||
|
||
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.
Почему ты делаешь проверку именно так?
Что будет, если context будет объектом, но не валидным?