javaScript创建对象的几种常见模式(工厂模式,构造函数模式,原型模式,构造原型组合模式)

一、工厂模式
缺点:没有解决对象识别的问题
优点:解决了创建多个相似对象的问题
         function createPerson(name,age,job){

            var o = new Object();

            o.name = name;

            o.age = age;

            o.job = job;

            o.sayname = function(){

                console.log(this.name);

            };

            return o;

       }

        var person1 = createPerson("qqq",21,"sss");

        person1.sayname();//qqq

        var person2 = createPerson("aaa",22,"sas");

        person2.sayname();//aaa



        console.log(person1 instanceof createPerson);//false

        console.log(person2 instanceof createPerson);//false

        console.log(person1.sayname === person2.sayname);//false

 

 
 
二、构造函数模式
优点:可以用来创建特定类型的对象,自定义的构造函数
缺点:每个 方法都要在每个实例上重新创建一遍。2个sayName方法不是同一个Function的实例。
        function Person(name,age,job){

            this.name = name;

            this.age = age;

            this.job = job;

            this.sayname = function(){

                console.log(this.name);

            };

       }

        var person1 = new Person("qqq",21,"sss");

        person1.sayname();//qqq

        var person2 = new Person("aaa",22,"sas");

        person2.sayname();//aaa

        console.log(person1 instanceof Person);//true

        console.log(person2 instanceof Person);//true

        console.log(person1.sayname === person2.sayname);//false
 
三、原型模式
优点:可以让所有对象实例共享它所包含的属性和方法。就是直接在原型对象中定义对象实例信息。
缺点:省略了构造函数传递初始化参数,结果所有实例在默认情况下都将取得相同的属性!
 
         function Person(){

        }



        Person.prototype.name = "unio";

        Person.prototype.age = "14";

        Person.prototype.job = "monitor";

        Person.prototype.sayname = function(){

               console.log(this.name);

        };

        var person1 = new Person();

        person1.sayname();//unio

        var person2 = new Person();

        person2.sayname();//unio



        console.log(person1 instanceof Person);//true

        console.log(person2 instanceof Person);//true

        console.log(person1.sayname === person2.sayname);//true

 

四、组合使用构造函数和原型模式
 优点:去掉构造模式和原型模式的缺点,集结二者之优点
         function Person(name,age,job){

             this.name = name;

             this.age = age;

             this.job = job;

        }



        Person.prototype.sayname = function(){

               console.log(this.name);

        };

        var person1 = new Person("qqq",21,"sss");

        person1.sayname();//qqq

        var person2 = new Person("aaa",22,"sas");

        person2.sayname();//aaa

        

        console.log(person1 instanceof Person);//true

        console.log(person2 instanceof Person);//true

        console.log(person1.sayname === person2.sayname);//true

 

 
五、动态原型模式
 
         function Person(name,age,job){

             this.name = name;

             this.age = age;

             this.job = job;

             if(typeof this.sayname != "function"){

                 Person.prototype.sayname = function(){

                       console.log(this.name);

                };

             }

        }

        var person1 = new Person("qqq",21,"sss");

        person1.sayname();//qqq

        var person2 = new Person("aaa",22,"sas");

        person2.sayname();//aaa



        console.log(person1 instanceof Person);//true

        console.log(person2 instanceof Person);//true

        console.log(person1.sayname === person2.sayname);//true

 

 
 

你可能感兴趣的:(JavaScript)