工厂模式

function Car(name) {
  this.name = name
  this.type = 'car'
}

function Bike(name) {
  this.name = name
  this.type = 'bike'
}


function Factory(type, name) {
 this.type = type 
}


Factory.prototype.getInstance = function() {
  if(this.type === 'car') {
    return new Car(this.name)
  }
  return new Bike(this.name)
}

var factory = new Factory('car', 'audio')
var car = factory.getInstance()

var factory2 = new Factory('bike', 'mybike')

var bike = factory2.getInstance()

results matching ""

    No results matching ""