How to implement refinement in JavaScript
<p>I am trying to implement (so-to-say) a refinement in JavaScript. Kindly look at the following example:</p>

<pre><code>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Function Override (Refinement) example&lt;/title&gt;
&lt;script type="text/javascript"&gt;

// original function
function oneFunc() {
    document.writeln("&lt;p&gt; In original oneFunc() &lt;/p&gt;");
}

var prevOneFunc = null;
if (oneFunc) {
    prevOneFunc = oneFunc;
}

// redeclared function (should refine/complement the original one)
function oneFunc() { 
    if (prevOneFunc) {
        prevOneFunc(); // should call original oneFunc(), BUT IT ISN'T
    }
    document.writeln("&lt;p&gt; In refined oneFunc() &lt;/p&gt;");
}

oneFunc();

&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>My intention was to have two printouts:</p>

<blockquote>
<pre><code>In original oneFunc()
In refined oneFunc()
</code></pre>
</blockquote>

<p>However, it seems that since at the <em>moment of execution</em> <code>oneFunc</code> refers to the new/refined function, hence the output is different than expected. In the debugger checked that I am entering into the infinite recursion :) (yes, understood why exactly).</p>

<p>Please explain which information I am missing to implement it properly. </p>

<p><strong>Update:</strong> A few limitations: I think (not sure) that I can't modify the original <code>oneFunc</code> declaration and it is declared just like described above. I shouldn't modify the declaration of the refined <code>oneFunc</code> either.</p>
