JavaScript 创建对象的几种方式

来源:互联网 发布:拒绝网络暴力演讲稿 编辑:程序博客网 时间:2024/06/04 18:15
<html><head>    <title>object-created</title>    <script type="text/javascript">    //1.factory pattern to create object, using this pattern we can not determine the type of the instance, it is just a instance of Object      var person1 = null;      var person2 = null;      /*      function createPerson(name,age){        var o = new Object();        o.name = name;        o.age = age;        o.sayName = function(){          alert(this.name);        }        return o;      }      person1 = createPerson('stone',21);      person1.sayName();      person2 = createPerson('jack',22);      person2.sayName();      alert(person1.sayName == person2.sayName);//false      alert(typeof person1);      alert(person1 instanceof Object);      alert(person1 instanceof createPerson);      //2.constructor pattern to create object, using this pattern we can determine the type of the instance, but you can the instance method is still different each other, this is waste of memory      function Person(name,age){        this.name = name;        this.age = age;        this.sayName = function(){          alert(this.name);        };      }      person1 = new Person('stone',21);      person1.sayName();      person2 = new Person('jack',33);      person2.sayName();      alert(person1.sayName == person2.sayName);//false      alert(person1 instanceof Object);//true      alert(person1 instanceof Person);//true            //3.prototype pattern to create object,everything seem to go well, but when there is a refrence attribute, when we upate one of the instance's attribute, all of the instances' attribute will be updated since they have the same method object      function Person(){      }      Person.prototype.name = "name";      Person.prototype.age = 21;      Person.prototype.friends = ['stone','jack'];      Person.prototype.sayName = function(){        alert(this.name);      };      person1 = new Person();      person1.sayName();      alert(person1.friends);//stone,jack      person2 = new Person();//stone,jack      person2.sayName();      alert(person2.friends);      alert(person1.sayName == person2.sayName);//true      alert(person1 instanceof Object);//true      alert(person1 instanceof Person);//true      person1.friends.push('jenny');      alert(person1.friends);//stone,jack,jenny      alert(person2.friends);//stone,jack,jenny            //4.combined use constructor and prototype, everything is ok, we normally use this pattern to create an object      function Person(name,age){        this.name = name;        this.age = age;        this.friends = ['stone','jack'];      }      Person.prototype.sayName = function(){        alert(this.name);      }      person1 = new Person('p1',22);      person1.sayName();      alert(person1.friends);      person2 = new Person('p2',23);      person2.sayName();      alert(person2.friends);      person1.friends.push('jenny');      alert(person1.friends);//stone,jack,jenny      alert(person2.friends);//stone,jack      */      //5.dynamic prototype pattern, it is so good      function Person(name,age){        this.name = name;        this.age = age;        if(typeof this.sayName != 'function'){          Person.prototype.sayName = function(){            alert(this.name);          };        }      }      person1 = new Person('stone',21);      person1.sayName();    </script></head><body>    </body></html>


0 0