JavaScript variables scope and underscore.js
<p>I have a function, with this inside it:</p>

<pre><code>var fobj = function(){};

var my_obj = fobj.extend({
    test: function(){
        this.year = 1999;  
        my_fmagic.doMagic([
            {
                field: 'year', 
                type: 'function', 
                value: function(data_from_doMagic){

                    console.log(data_from_doMagic, this.year);

                    return data_from_doMagic == this.year;
                }
            }
        ]);
    }
});
</code></pre>

<p>Then, the <code>doMagic</code> function:</p>

<pre><code>var fmagic = function(){};

var my_fmagic = fmagic.extend({
    doMagic: function( rules ) {
        var data_from_doMagic = 1999;
        _.each(rules, function(rule) { 
            switch(rule.type) {
                case "function":
                    var f = _.wrap(rule.value, function(func){
                        return func( data_from_doMagic );
                    });
                    f();
                    break;
            }
        });
    }
});
</code></pre>

<p>The idea is: pass an array of "rules" to <code>doMagic</code> and let it do the rest.</p>

<p>Expected result: <code>test()</code> should return <code>true</code> (well, in this example it won't return anything, obviously, because there's no return, but the <code>value</code> field of the rule should be <code>true</code>.) </p>

<p>Problem: Instead the result being <code>true</code> it's <code>false</code>, because the function <code>f()</code> in <code>doMagic</code> doesn't know what the variable <code>year</code> is, and so it compares "1999" with "undefined".</p>

<p>As you can see I know where the problem is, but I can't think about any solution.</p>

<p>Ideas?</p>

<p>Regards</p>
