工厂模式

简单工厂模式

<script>
        var factory=function(name,age,sex,national){
            this.name=name
            this.age=age
            this.sex=sex
            switch(national){
                case "Chinese":
                    this.speak=function(){
                        console.log("我是"+this.name+"我今年:"+this.age+"我是中国人")
                    }
                    break;
                case "England":
                    this.speak=function(){
                        console.log("我是"+this.name+"我今年:"+this.age+"我是英国人")
                    }
                    break;
            }
        }
        var person=new factory("jack",15,male,"Chinese")
        person.speak()
    </script>

抽象工厂模式

<script>
        function myCar(color,price){
            this.color=color
            this.price=price
        }
        myCar.prototype.run=function(){
            throw new Error("我是摸具,具体实现自己决定" )
        }

        var BM=function(color,price){
            myCar.call(this,color,price)
        }
        BM.prototype.run=function(){
            console.log("我是宝马车我的颜色是"+this.color+"我的价格是"+this.price+"宝马正在跑")
        }
        new BM('red','1234').run()



        var BC=function(color,price){
            myCar.call(this,color,price)
        }
        BC.prototype.run=function(){
            console.log("我是奔驰车我的颜色是"+this.color+"我的价格是"+this.price+"奔驰正在跑")
        }
        new BC('blue','99999').run()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值