JavaScript 创建对象的几种方式

<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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值