多谢楼上的回答。 其实遇到的问题是: function abc() { if (abc.count == undefined) abc.count = 0 alert(abc.count++); } a = new abc(); b = new abc(); 如果把abc()的两行代码的abc.count同时改为abc.prototype.count的话,效果相同,但只把其中一行改变的话,就得不到预期效果,不知道为什么。。
function abc() { if (abc.count == undefined) abc.count = 0 alert(abc.count++); alert(this.count); }abc.prototype.count = 2; a = new abc(); b = new abc(); abc.prototype.count相当于函数内部得this.count 而abc.count只不过是一个全局变量而已
其实遇到的问题是:
function abc()
{
if (abc.count == undefined) abc.count = 0
alert(abc.count++);
}
a = new abc();
b = new abc();
如果把abc()的两行代码的abc.count同时改为abc.prototype.count的话,效果相同,但只把其中一行改变的话,就得不到预期效果,不知道为什么。。
{
if (abc.count == undefined) abc.count = 0
alert(abc.count++);
alert(this.count);
}abc.prototype.count = 2;
a = new abc();
b = new abc();
abc.prototype.count相当于函数内部得this.count
而abc.count只不过是一个全局变量而已