• 谈javascript继承


      设计类的时候我们都希望减少重复性的代码,并且弱化对象间的耦合,这也是继承能带来的好处。担心继承也有缺点,让一个类继承另一个类会导致两者产生强耦合。我们将讨论一些有助于避免这种问题的技术,以下三种继承:

    1,类式继承

    首先创建构造函数

    function Person(name) {
      this.name = name;
    }
    
    Person.prototype.getName = function() {
      return this.name;
    }
    
    var reader = new Person('John Smith');
    reader.getName();

    原型链继承父类Person

    function Author(name,books){
    	Person.call(this,name);
    	this.books = books;
    }
    Author.prototype = new Person();
    Author.prototype.constructor = Author; //这里有个需要特别指出的地方,当Author的原型指向Person的一个实例时,它的构造函数不在指向它自己,需要手动设为Auther
    Author.prototype.getbooks = function() {
    	return this.books;
    }
    

    可以使用extend方法来统一继承分类的原型方法和属性

    function extend(subClass, superClass) {
      var F = function() {};
      F.prototype = superClass.prototype;
      subClass.prototype = new F();
      subClass.prototype.constructor = subClass;
    
      subClass.superclass = superClass.prototype;
      if(superClass.prototype.constructor == Object.prototype.constructor) {
        superClass.prototype.constructor = superClass;
      }
    }
    

      

    总结类式继承的代码

    function Author(name, books) {
      Author.superclass.constructor.call(this, name); 
      this.books = books;
    }
    extend(Author, Person);
    
    Author.prototype.getBooks = function() {
      return this.books;
    };
    
    Author.prototype.getName = function() {
      var name = Author.superclass.getName.call(this);
      return name + ', Author of ' + this.getBooks().join(', ');
    };
    

     2,原型式继承

    function clone(object){
    	function F(){};
    	F.prototype = object;
    	return new F;
    }
    var Person = {
      name: 'default name',
      getName: function() {
        return this.name;
      }
    };
    
    var reader = clone(Person);    //继承单一模式的对象字面量父类
    

      相比类式继承,原型继承更能节约内存,因为在类式继承方式中每一个对象在内存中都有自己的一套私有属性和方法。原型继承相对更为简练。

    3,掺元类

    function augment(receivingClass, givingClass) {
      if(arguments[2]) { // Only give certain methods.
        for(var i = 2, len = arguments.length; i < len; i++) {
          receivingClass.prototype[arguments[i]] = givingClass.prototype[arguments[i]];
        }
      } 
      else { // Give all methods.
        for(methodName in givingClass.prototype) { 
          if(!receivingClass.prototype[methodName]) {
            receivingClass.prototype[methodName] = givingClass.prototype[methodName];
          }
        }
      }
    }
    var Person = function(){};
    Person.prototype = {
      name: 'default name',
      getName: function() {
        return this.name;
      }
    };
    function Author(books){
      this.books = books;
    }
    augment(Author,Person);
    var au = new Author('hhh')
    

      

    掺元类更适用于扩充一个类,这是一种避免出现重复性代码的轻便的解决办法。

  • 相关阅读:
    20155201 第十二周课堂实践
    20155201 实验三《Java面向对象程序设计》实验报告
    20155201 第十一周Java课堂实践
    20155201 2016-2017-2 《Java程序设计》第十周学习总结
    20155201 2016-2017-2 《Java程序设计》第九周学习总结
    20155201 实验二《Java面向对象程序设计》实验报告
    Keras官方中文文档:Keras安装和配置指南(Windows)
    Keras官方中文文档:Keras安装和配置指南(Linux)
    Keras官方中文文档:常见问题与解答
    TensorLayer官方中文文档1.7.4:API – 数据预处理
  • 原文地址:https://www.cnblogs.com/55555azhe/p/2875222.html
Copyright © 2020-2023  润新知