我開始使用模式觀察器來開發我的jQuery應用程序,但真誠地,我不明白這種模式的好處。jQuery和觀察者模式
例子:
myfunctions = {
first_function: function() {
alert('This is the first function');
},
second_function: function() {
alert('This is the second function');
}
};
現在,爲什麼這個方法:
$(document).bind({
'first_function': myfunctions.first_function,
'second_function': myfunctions.second_function
});
$('button').bind('click', function() {
$(document).trigger('first_function');
});
是比這更好:
$('button').click(function() {
myfunctions.first_function();
});
爲什麼在開始理解之前就開始使用它? –