staticMethod_private

by xiaoji121

JavaScript

// 构造函数
var Gadget = (function(){

    // 静态变量/属性
    var counter = 0
        ,newGadget;

    // 这将成为新的构造函数的实现
    newGadget = function(){
        counter += 1;
    };

    // 特权方法
    newGadget.prototype.getLastId = function(){
        return counter;
    }

    return newGadget;
})();

var iphone = new Gadget();
alert(iphone.getLastId());// 1

var ipod = new Gadget();
alert(ipod.getLastId());// 2