javascript设计模式(二)--闭包实现

类的静态变量通过闭包来实现

//
var Book = (function(){
  
  //
  var bookNum = 0;
  //
  function checkBook(name){

  }
  //
  return function(newId,newName,newPrice){
    var name,price;

    function checkID(id){}

    this.getName = function(){};
    this.getPrice = function(){};
    this.setName = function(){};
    this.setPrice = function(){};

    this.id = newId;

    this.copy = function(){};

    bookNum++

    if(bookNum > 100)
      throw new Error('only sale 100');

    this.setName(name);
    this.setPrice(price);
  }
})

Book.prototype = {
  
  isJSBook : false,
  display : function(){}
}


你可能感兴趣的:(javascript设计模式(二)--闭包实现)