【javascript】javascript設計模式之原廠模式

來源:互聯網
上載者:User

標籤:bsp   原型繼承   instance   prot   javascrip   方法   doc   throw   typeof   

【簡單原廠模式】:同種類

var cat = function () {        this.hh = ‘高冷‘    }    cat.prototype = {        pin: function () {            console.log("白色");        }    }    var dog = function () {        this.hh = ‘粘人‘    }    dog.prototype ={        pin: function () {            console.log("黃色");        }    }    var ren = function(pet){        switch (pet){            case ‘qinjin‘:                return new dog();            case ‘gaoleng‘:                return new cat();        }    }   var guke =  ren(qinjin)    console.log(guke);function cat(name,pinzhong,price) {    var o = new Object();    o.name = name;    o.pinzhong = pinzhong    o.price = price    o.getName = function () {        console.log(this.name);    }    return o;}var meiduan = cat(‘xiaoduanduan‘, ‘meiduan‘, 2000)var jiafei = cat(‘xiaofeifei‘,‘feifei‘, 9000)    meiduan.getName();    jiafei.getName()

 

【Factory 方法模式】

   var Factory = function (type,content) {        if(this instanceof Factory){            var s = new this[type](content)            return s        }else{            return new Factory(type, content)        }    }    Factory.prototype = {        dog: function (content) {            this.content = content;            (function (content) {                var div = dosumnet.createElement(‘div‘);                div.innerHTML = content;                div.style.border = ‘1px solid red‘                document.getElementById(‘conta‘)            })(content)        },        lv: function (content) {        }    }    var data = [        {type:‘dog‘,content:‘我是一隻狗‘},        {type:‘lv‘,content:‘我是一隻狗‘}    ]    for(var i = 2; i>0;i--){        Factory(s[i].type,s[i].content)    }

  

【抽象原廠模式】

//  抽象原廠模式 每個子類都有一個繼承var home = function (subType,superType) {    //  判斷抽象工廠中是否有該對象類    if (typeof home[superType] === ‘function‘){        //  緩衝類        function F() {            this.type=‘戶型‘        }        //  繼承父類屬性和方法        F.prototype = new home[superType];        //  將自雷constructor指向子類        subType.constructor = subType        //  子類原型繼承"父類"        subType.prototype = new F();    }else{        //  不存在該抽象類別拋出錯誤        throw  new Errow(‘未建立該抽象類別‘)    }}//  別墅類home.villa = function () {    this.type = ‘villa‘    console.log(‘car‘, this);}home.villa.prototype = {    getPrice: function () {    },    gethuxing: function () {            }}//  低檔住宅var swimvilla = function (mianji,huxing) {    this.mianji = mianji;    this.huxing = huxing;}//  抽象工廠實現對villa抽象類別的繼承home(swimvilla, ‘villa‘);swimvilla.prototype.getPrice = function () {    console.log(this.price)}swimvilla.prototype.gethuxing = function () {    console.log(this.huxing);}const swimvilla1 = new swimvill(100,‘5室‘)swimvilla1.getPrice()

 

【javascript】javascript設計模式之原廠模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.