hay / stapes

the Javascript MVC microframework that does just enough
http://hay.github.io/stapes
MIT License
443 stars 54 forks source link

Private var work like static private var #40

Closed gizur-ess-anshuk closed 11 years ago

gizur-ess-anshuk commented 11 years ago

Try this:

var ClassA = (function() {

    /**
     * Private Variables
     */
    var _a;

    var ClassA = Stapes.subclass({

        constructor : function(argA) {
            _a = argA;
        },

        getA: function() {
            return (_a);
        }

    });

    return ClassA;
})();

var a1 = new ClassA("foo");
var a2 = new ClassA("bar");

console.log(a1.getA()); //bar
console.log(a2.getA()); //bar
hay commented 11 years ago

This is a Javascript problem, not a Stapes one. The problem is that the scope of ClassA stays the same in both iterations. A new doesn't give you a new anonymous scope. Unfortunately there's no easy way to solve this, at least, not in Stapes. You could try using pseudo-private variables by adding them to the class but using an underscore (ClassA._private = private;).

I'll add some comments in the docs on this though.

Thanks for your issue!