Dynamically Generated Methods in JavaScript
<p>While searching the web, I've encountered a <a href="http://arrix.blogspot.com/2007/01/several-controversial-points-in-pro.html" rel="nofollow">post</a> that shows why the following example of dynamically generated methods does not work as planned:</p>

<pre><code>// Create a new user object that accepts an object of properties
function User( properties ) {
    // Iterate through the properties of the object, and make sure
    // that it's properly scoped (as discussed previously)
    for ( var i in properties ) { (function(){
        // Create a new getter for the property
        this[ "get" + i ] = function() {
            return properties[i];
        };
        // Create a new setter for the property
        this[ "set" + i ] = function(val) {
            properties[i] = val;
        };
    })(); }
}
</code></pre>

<p>The reason for that is the anonymous function, which uses the "this" keyword is context of the "window", instead of "User".</p>

<p>1) Why does the this keyword in the anonymous function refers to "window instead of "User"?</p>

<p>2) Is there an accepted and common way to create "Dynamically Generated Methods"?</p>

<p>Thanks,</p>

<p>Joel</p>
