我一直在尝试在javascript中模拟静态属性.在几个地方已经提到过class.prototype.property在从类继承的所有对象中都是静态的.但我的POC说不然.请看一下:
使用Class.prototype.property
//Employee class
function Employee() {
    this.getCount = function(){
        return this.count; 
    };      
    this.count += 1;
}
Employee.prototype.count = 3;
var emp = [], i;
for (i = 0; i < 3; i++) {
    emp[i] = new Employee();
    console.log("employee count is "+ emp[i].getCount());
}
/*Output is:
employee count is 4
employee count is 4
employee count is 4*/
Run Code Online (Sandbox Code Playgroud)
我的问题#1:如果这是静态的,那么count的值不应该是4,5,6等,因为所有对象共享相同的count变量?
然后我用Class.prototype做了另一个POC,我认为这是静态的.
使用Class.property
//Employee class
function Employee() {
    this.getCount = function(){
        return Employee.count; 
    };      
    Employee.count++;
}
Employee.count = 3;
var emp = …Run Code Online (Sandbox Code Playgroud)