首页 > 技术文章 > js设计模式-工厂模式

jsydb 2018-09-05 09:38 原文

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>工厂模式</title>
</head>
<body>
<script>
    /*
    给一个操作的集合提供一个统一访问的接口
     */
    class Shop {
        constructor(name){
            if(typeof this[name] === 'function'){
                this[name]()
            }
            else {
                throw new Error("本商店没有该水果")
            }
        }
        Apple(){
            this.name = "苹果";
            this.prcie = 10
        }
        Orange(){
            this.name = "橘子";
            this.price = 10
        }
    }

    console.log(new Shop("Apple"));
    console.log(new Shop("Orange"));
    console.log(new Shop("香蕉"));
</script>
</body>
</html>

 

推荐阅读