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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章