65. 使用工厂模式创建对象

<!DOCTYPE html>
<html>
     <head>
         <meta charset=”UTF-8″>
         <title></title>
         <script type=”text/javascript”>
            
             /*
              * 创建一个对象
              */
             var obj = {
                     name:”孙悟空”,
                     age:18,
                     gender:”男”,
                     sayName:function(){
                         alert(this.name);
                     }
             };

            /*
              * 使用工厂方法创建对象
              *     通过该方法可以大批量的创建对象
              */
             function createPerson(name , age ,gender){
                 //创建一个新的对象
                 var obj = new Object();
                 //向对象中添加属性
                 obj.name = name;
                 obj.age = age;
                 obj.gender = gender;
                 obj.sayName = function(){
                     alert(this.name);
                 };
                 //将新的对象返回
                 return obj;
             }
            
             /*
              * 用来创建狗的对象
              */
             function createDog(name , age){
                 var obj = new Object();
                 obj.name = name;
                 obj.age = age;
                 obj.sayHello = function(){
                     alert(“汪汪~~”);
                 };
                
                 return obj;
             }
            
             var obj2 = createPerson(“猪八戒”,28,”男”);
             var obj3 = createPerson(“白骨精”,16,”女”);
             var obj4 = createPerson(“蜘蛛精”,18,”女”);
             /*
              * 使用工厂方法创建的对象,使用的构造函数都是Object
              *     所以创建的对象都是Object这个类型,
              *     就导致我们无法区分出多种不同类型的对象
              */
             //创建一个狗的对象
             var dog = createDog(“旺财”,3);
             var dog1 = createDog(“旺财1”,31);
             var dog2 = createDog(“旺财2”,32);

            console.log(dog);
             console.log(dog1);
             console.log(dog2);
             console.log(obj4);
            
            
         </script>
     </head>
     <body>
     </body>
</html>

打赏

发布者

张 大鹏

北京理想国惠玩科技有限公司CEO

发表评论

电子邮件地址不会被公开。 必填项已用*标注