就私有静态成员而言,指的是成员具有如下属性:
1.以同一个构造函数创建的所有对象共享该成员。
2.构造函数外部不可访问该成员。
//构造函数
var Gadget = (function(){
//静态变量/属性
var counter = 0,
NewGadGet;
NewGadget = function(){
counter++;
}
NewGadget.prototype.getLastId = function(){
return counter;
}
return NewGadget;
}())
var iphone = new Gadget();
console.log( iphone.getLastId() ); //输出1
var iPad = new Gadget();
console.log( iPad.getLastId() ); //输出2
var iPod = new Gadget();
console.log( iPod.getLastId() ); //输出3