工厂模式

  • 💡思路

    1. 创建需要批量的构造函数
    2. 通过工厂中间函数,根据一些固定的输入模式,返回相应的输出
  • 🔨实现

    function Student(name, subjects){
        this.name = name
        this.subjects = subjects
    }
    function factory(name, type){
        switch(type){
            case '文科':
            return new Student(name, ['历史','政治','地理'])
            break
            case '理科':
            return new Student(name, ['物理','化学','生物'])
            default:
            throw "找不到该专业"
        }
    }
    let zs = factory('张三','文科')
    let ls = factory('李四','理科')
    console.log(zs)
    console.log(ls)