6-工厂模式

<!DOCTYPE html>
<html lang='en'>

<head>
    <meat charset='UTF-8' />
    <meat name='viewport' content='width=device-width, initial-scale=1.0' />
    <style>

    </style>
</head>

<body>
    <div id="show">

    </div>



    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
    <script>
        // 工厂方法模式
        function PlaneFactory() {

        }

        // 创建对象接口
        PlaneFactory.creat = function (type) {
            // 判断原型链上是否存在该方法
            if (PlaneFactory.prototype[type] == undefined) {
                throw new Error('not this constructor');
            }

            /**
                1.通过new 创建的函数,其原型链一定指向本身,不管是不是子类,除非修改原型链指向
                2.修改原型链的目的是为了让原型链上挂载方法的原型链指向 PlaneFacetory
                    这样就能继承父类定义的方法,也就能使用公共方法
            */
            if (PlaneFactory.prototype[type].constructor !== PlaneFactory) {
                PlaneFactory.prototype[type].prototype = new PlaneFactory();
            }
            let args = [].slice.call(arguments, 1);
            let newPlane = new PlaneFactory.prototype[type](...args);
            return newPlane;
        }


        // 公共方法---this.toch,写在原型链上,避免代码冗余
        PlaneFactory.prototype.toch = function () {
            this.toch = function () {
                this.blood -= 50;
                if (this.blood == 0) {
                    console.log("------over-----")
                }
            }
        }
        PlaneFactory.prototype.die = function() {
            console.log('该飞机已被击毁-------');
        }

        // 创建小飞机
        PlaneFactory.prototype.SmallPane = function (x, y) {
            this.x = x;
            this.y = y;
            this.width = 100;
            this.height = 100;
            this.blood = 100;
            this.name = 'SmallPane';
            // this.toch = function () {
            //     this.blood -= 50;
            //     if (this.blood == 0) {
            //         console.log("------over-----")
            //     }
            // }
        }
        // 创建大飞机
        PlaneFactory.prototype.BigPlane = function (x, y) {
            this.x = x;
            this.y = y;
            this.width = 150;
            this.height = 200;
            this.blood = 200;
            this.name = 'BigPlane';
            // this.toch = function () {
            //     this.blood -= 50;
            //     if (this.blood == 0) {
            //         console.log("------over-----")
            //     }
            // }
        }
        // 创建攻击型飞机
        PlaneFactory.prototype.AttackPlane = function (x, y) {
            this.x = x;
            this.y = y;
            this.width = 100;
            this.height = 125;
            this.blood = 100;
            this.name = 'AttackPlane';
            // this.toch = function () {
            //     this.blood -= 50;
            //     if (this.blood == 0) {
            //         console.log("------over-----")
            //     }
            // }
            this.attck = function () {
                console.log("------biu~ biu~ biubiu!-----")
            }
        }
        let oAp = PlaneFactory.creat('AttackPlane', 20, 30);
        let oBp = PlaneFactory.creat('BigPlane', 20, 30);
        let oSp = PlaneFactory.creat('SmallPane', 40, 50);


    </script>
</body>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值