Adding behavior to a function in JavaScript?
<p>Say I am writing a web-app, and I'd like to add certain behavior to a function after it has been declared in multiple places without overriding the old behaviors of the function.</p>

<p>Here is some simplified code (notice the <strong>window.onmousedown</strong> function</p>

<pre><code>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();
</code></pre>

<p>In this case, window.onmousedown will not do both things it was meant to do - the definition it has in createSidebar will override its definition from createMenu. Is there a standard way of achieving a sort of behavior in which windows.onmousedown will retain both behaviors?</p>
