Web アプリを作成していて、関数の古い動作をオーバーライドせずに、複数の場所で宣言された後に関数に特定の動作を追加したいとします。
以下は単純化されたコードです ( window.onmousedown関数に注意してください)。
var createMenu = function(){
var menu = document.createElement('ul');
/* add things to the menu */
window.onmousedown = function(e){
menu.style.background = "red";
}
}
var createSidebar = function(){
var sidebar = document.createElement('div');
/* add things to the sidebar */
window.onmousedown = function(e){
sidebar.id = "clicked";
}
}
var createMenu();
var createSidebar();
この場合、window.onmousedown は意図された両方のことを実行しません。createSidebar にある定義は、createMenu からの定義をオーバーライドします。windows.onmousedown が両方の動作を保持するような動作を実現する標準的な方法はありますか?