I'm new to front-end development and now I'm learning JavaScript with jQuery. I couldn't understand JavaScript's classes and objects and jQuery plugins, but after reading some articles and examples I wrote simple notifications system.
(function ($) {
var notification = function (title, msg, duration) {
var self = this;
this.hide = function () {
self.element.fadeOut(1000, function () {
self.element.remove();
})
}
this.element = $('<div class="notification"><h3>' + title + '</h3><div class="notification_body">' + msg + '<br/><i>(double click to hide)</i></div></div>');
this.element.dblclick(self.hide);
if (duration !== undefined) {
setTimeout(function () {
self.hide();
}, duration * 1000);
}
$('#notifications_bar').prepend(this.element.hide().fadeIn(1000));
};
$.extend({
notify:function (title, msg, duration) {
return new notification(title, msg, duration);
}
});
Example:
$(function() {
$.notify('Title', 'Message'); // sticky note
$.notify('Title', 'Message', 2); // 2-seconds notification
});
The most terrible were closures and this
keyword. I couldn't understand how to use them, so I want to know if there in my code above 'bad-practice' parts.