我在这里有这段代码:
var Person = (function() {
var name;
var PersonConstructor = function(n) {
name = n;
};
PersonConstructor.prototype.getName = function() {
return name;
};
return PersonConstructor;
})();
var people = [];
var person1 = new Person("Foo");
var person2 = new Person("Bar");
alert(person1.getName()); // I want it to be Foo
people.push(person1);
people.push(person2);
我从这里得到了模拟类的想法。. var name;
但是当然,我忽略了私有变量也是静态变量的事实。由于这阻碍了我目前的努力,我想知道是否有办法在这个例子中保持私人行为但避免静态行为?