js面向对象(2)

2018-01-31  本文已影响0人  悟空你又瘦了

4.js里面的继承

1.混入式继承(mix-in)
//继承1个对象
var o = {
           extend: function(obj){
            for(var k in obj){
               this[k] = obj[k];
             }
          }
       };
  o.extend(o1);
//继承多个对象
   var o = {
           extend: function(){
               for (var i = 0; i < arguments.length; i++) {
                   var obj = arguments[i];

                   for(var k in obj){
                       this[k] = obj[k];
                   }
               }
           }
       }

   o.extend(o1, o2, o3, o4);

2.经典继承(Object.create)
语法:var obj = Object.create(obj1);
含义:创建一个对象obj,并且继承了obj1
不兼容ie8以下
 function myCreate (obj){
        if (Object.creat) {
            return Object.creat(obj)
        } else{
            function F(){};
            F.prototype = obj;
            return new F ();
        }
        
    }
3.继承的方式也有很多种:原型链继承、借用构造函数继承、组合继承、原型式继承、寄生式继承、寄生组合式继承……

WTF ! 简直要吐血有木有

那就用一种自己最熟练的就好了,形成自己的风格吧!


//组合使用构造函数模式和原型模式  创建对象
//父类
function Person(name){
    this.name = name || "Tom";
    this.age = "12";
    //实例方法 (不会被覆盖)
    this.say = function(){
        console.log("Hello " + this.name + "!");
    }
}
//原型方法
Person.prototype.run = function(){
    console.log(this.name + " is running");
}
Person.prototype.eat = function(food){
    console.log(this.name + " is eating " + food);
}

/*更方便的写法:
 * Person.prototype = {   //这里相当于完全重写了默认的prototype对象,constructor属性也变成了新的对象的constructor属性(指向Object)
    constructor: Person,  //将构造器重新指向Person
    run: funtion(){
        console.log(this.name + " is running");
    },
    eat: function(){
        console.log(this.name + " is eating " + food);
    }
}*/

//组合继承:通过调用父类构造函数,继承父类的属性并保留传参的优点,然后通过将父类实例作为子类原型,实现函数复用
//子类
function People(name){
    Person.call(this);  //构造函数继承 (调用父类构造函数)
    this.name = name || "Rose"; //修改继承的属性
    this.sport = "football";  //自定义新的属性
}

People.prototype = new Person(); //原型继承 (将父类实例作为子类原型)

People.prototype.say = function(){  //修改继承的方法
    console.log("Hello " + this.name + " you are welcome");
}

People.prototype.run = function(){  //修改继承的方法
    console.log(this.name + " is go go go");
}

People.prototype.play = function(){  //自定义新的方法
    console.log(this.name + " is playing " + this.sport);
}

//test
var person1 = new People("Jack");
console.log(person1.name);      //Jack
console.log(person1.age);       //12                  //继承原型的属性
person1.say();                  //Hello Jack!   //实例方法无法在其原型上修改。  如此修改:person1.say(){ balabala }
person1.run();                  //Jack is go go go    //覆盖了原型方法
person1.eat("apple");           //Jack is eating apple

var person2 = new People("Lucy");
console.log(person2.name);      //Lucy
console.log(person2.age);       //12                  //继承原型的属性
person2.say();                  //Hello Lucy!   //实例方法无法在其原型上修改 
person2.run();                  //Lucy is go go go    //覆盖了原型方法
person2.eat("apple");           //Lucy is eating apple
person2.play();                 //Lucy is playing football
上一篇下一篇

猜你喜欢

热点阅读