美文网首页
工厂模式

工厂模式

作者: 清酒金杯空对月 | 来源:发表于2018-12-12 18:00 被阅读0次

    工厂模式
    function Person(name,age,job){
    //创建对象
    var o = {};
    o.name = name;
    o.age = age;
    o.job = job;

                       o.showName = function(){
                                    alert(this.name);
                      };
                        .showName = function(){
                                     alert(this.age);
                      };
                       .showName = function(){
                                     alert(this.age)
                       };
                        return o;
    }
    var Tom = Person('tom',18,'程序员');
    Tom.showName();
    

    构造函数
    function Person(name,age,job){
    this.name = name;
    this.age = age;
    this.job = job;

                       this.showName = function(){
                                    alert(this.name);
                      };
                       this .showAge = function(){
                                     alert(this.age);
                      };
                       this.showJob = function(){
                                     alert(this.age)
                       };
                        return o;
    }
    var Bob = new Person('bob',18,'产品');
    Bob.showName();
    

    3.原型模式
    function Person(name,age,job){
    this.name = name;
    this.age = age;
    this.job = job;
    }
    //prototype原型
    person.peototype.showName = function(){
    alert(this.name);
    };
    person.peototype.showAge = function(){
    alert(this.age);
    };
    person.peototype.showJob = function(){
    alert(this.job);
    };
    var Lucy = new person('Lucy',19,'测试鼠');
    alert(luck.showName());

    相关文章

      网友评论

          本文标题:工厂模式

          本文链接:https://www.haomeiwen.com/subject/zuuphqtx.html