js--创建对象与对象继承

2017-03-20  本文已影响0人  栗子酥小小

创建对象

  1. 工厂模式:
    function createPerson(name, age, job){
    var o = new Object();
    o.name = name;
    o.age = age;
    o.job = job;
    o.sayName = function(){
    alert(this.name);
    };
    return o;
    }
    var person2 = createPerson("Greg", 27, "Doctor");
  1. 正常构造函数模式:
    function Person(name, age, job){
    this.name = name;
    this.age = age;
    this.job = job;
    this.sayName = function(){
    alert(this.name);
    };
    }
    var person2 = new Person("Greg", 27, "Doctor");
  1. 特殊比较——寄生构造函数模式:
    function Person(name, age, job){
    var o = new Object();
    o.name = name;
    o.age = age;
    o.job = job;
    o.sayName = function(){
    alert(this.name);
    };
    return o;
    }

     var friend = new Person("Nicholas", 29, "Software Engineer");
     friend.sayName();  //"Nicholas"
    
  1. 原型模式:
    function Person(){
    }

     Person.prototype.name = "Nicholas";
     Person.prototype.age = 29;
     Person.prototype.job = "Software Engineer";
     Person.prototype.sayName = function(){
         alert(this.name);
     };
     
     var person1 = new Person();
     var person2 = new Person();
     
     person1.name = "Greg";
     alert(person1.name);   //"Greg" – from instance
     alert(person2.name);   //"Nicholas" – from prototype
    

对象继承

  1. 原型继承:
    function SuperType(){
    this.property = true;
    }

     SuperType.prototype.getSuperValue = function(){
         return this.property;
     };
     
     function SubType(){
         this.subproperty = false;
     }
     
     //inherit from SuperType
     SubType.prototype = new SuperType();
     
     SubType.prototype.getSubValue = function (){
         return this.subproperty;
     };
     
     var instance = new SubType();
     alert(instance.getSuperValue());   //true
    
     alert(instance instanceof Object);      //true
     alert(instance instanceof SuperType);   //true
     alert(instance instanceof SubType);     //true
    
     alert(Object.prototype.isPrototypeOf(instance));    //true
     alert(SuperType.prototype.isPrototypeOf(instance)); //true
     alert(SubType.prototype.isPrototypeOf(instance));   //true
    
  1. 借用构造函数:
    function SuperType(){
    this.colors = ["red", "blue", "green"];
    }

     function SubType(){  
         //inherit from SuperType
         SuperType.call(this);
     }
    
     var instance1 = new SubType();
     instance1.colors.push("black");
     alert(instance1.colors);    //"red,blue,green,black"
     
     var instance2 = new SubType();
     alert(instance2.colors);    //"red,blue,green"
    
  1. 组合继承(原型链+借用构造函数)

     function SuperType(name){
         this.name = name;
         this.colors = ["red", "blue", "green"];
     }
     
     SuperType.prototype.sayName = function(){
         alert(this.name);
     };
    
     function SubType(name, age){  
         SuperType.call(this, name);  //第二次调用超类构造函数
         
         this.age = age;
     }
    
     SubType.prototype = new SuperType();    //第一次调用超类构造函数
      SubType.prototype.constructor = SubType;
     SubType.prototype.sayAge = function(){
         alert(this.age);
     };
     
     var instance1 = new SubType("Nicholas", 29);
     instance1.colors.push("black");
     alert(instance1.colors);  //"red,blue,green,black"
     instance1.sayName();      //"Nicholas";
     instance1.sayAge();       //29
    
     var instance2 = new SubType("Greg", 27);
     alert(instance2.colors);  //"red,blue,green"
     instance2.sayName();      //"Greg";
     instance2.sayAge();       //27
    
  1. 原型式继承:
    function object(o){
    function F(){};
    F.prototype = o;
    return new F();
    }
  1. 原型式继承进阶版——寄生式继承
    function createAnother(original){
    var clone = object(original); //这里沿用上面的object()函数
    clone.sayHi = function(){
    alert("hi");
    }
    return clone;
    }
    var person = {
    name: "json",
    age: 23
    }
    var newPerson = createAnother(person);
    newPerson.sayHi(); //"hi"
  1. 寄生组合式继承:

补充:寄生组合式继承的图解:

111 (1).png
上一篇 下一篇

猜你喜欢

热点阅读