zoukankan      html  css  js  c++  java
  • js深入研究之无法理解的js类代码,extend扩展

    <script type="text/javascript">
    function Person(name) {
      this.name = name;
    }
    
    Person.prototype.getName = function() {
      return this.name;
    }
    
    function Author(name, books) {
      Person.call(this, name); // 定义:调用一个对象的一个方法,以另一个对象替换当前对象。
      this.books = books; // Add an attribute to Author.
    }
    
    Author.prototype = new Person(); // 设置原型链
    Author.prototype.constructor = Author; // 设置构造属性
    Author.prototype.getBooks = function() { // 添加方法
      return this.books;
    };
    
    var author = [];
    author[0] = new Author('Dustin Diaz', ['JavaScript Design Patterns']);
    author[1] = new Author('Ross Harmes', ['JavaScript Design Patterns']);
    
    alert(author[0].getName()); //输出 Dustin Diaz
    alert(author[0].getBooks()); //输出 JavaScript Design Patterns
    alert(author[1].getName()); //输出 Ross Harmes
    alert(author[1].getBooks()); //输出 JavaScript Design Patterns
    </script>

    功力不够,无法理解

    进一步升级提取

    <script type="text/javascript">
    /* 扩展函数 */
    function extend(subClass, superClass) {
      var F = function() {};
      F.prototype = superClass.prototype; // F已成superClass父类
      subClass.prototype = new F(); //子类继承父类的原子
      subClass.prototype.constructor = subClass;
    }
    
    
    /* Person类 */
    
    function Person(name) {
      this.name = name;
    }
    
    Person.prototype.getName = function() {
      return this.name;
    }
    
    /* Author类 */
    
    function Author(name, books) {
      Person.call(this, name);
      this.books = books;
    }
    extend(Author, Person);
    
    Author.prototype.getBooks = function() {
      return this.books;
    };
    
    
    var author = []; //定义数组
    author[0] = new Author('Dustin Diaz', ['JavaScript Design Patterns']);
    author[1] = new Author('Ross Harmes', ['JavaScript Design Patterns']);
    
    alert(author[0].getName()); //输出 Dustin Diaz
    alert(author[0].getBooks()); //输出 JavaScript Design Patterns
    alert(author[1].getName()); //输出 Ross Harmes
    alert(author[1].getBooks()); //输出 JavaScript Design Patterns
    </script>

     进一步改进,太牛逼了,作者

    <script type="text/javascript">
    /* 扩展函数 */
    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;
      }
    }
    
    
    /* Person类 */
    
    function Person(name) {
      this.name = name;
    }
    
    Person.prototype.getName = function() {
      return this.name;
    }
    
    /* Author类 */
    
    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(', ');
    };
    
    var author = []; //定义数组
    author[0] = new Author('Dustin Diaz', ['JavaScript Design Patterns']);
    author[1] = new Author('Ross Harmes', ['JavaScript Design Patterns']);
    
    alert(author[0].getName()); //输出 Dustin Diaz , Author of JavaScript Design Patterns
    alert(author[0].getBooks()); //输出 JavaScript Design Patterns
    alert(author[1].getName()); //输出 Ross Harmes , Author of JavaScript Design Patterns
    alert(author[1].getBooks()); //输出 JavaScript Design Patterns
    </script>
  • 相关阅读:
    假期训练七(hdu-2845 dp,hdu-1846,2188 巴什博奕)
    假期训练六(poj-1753,递归+hdu-2844,多重背包)
    假期训练五(poj-1077bfs+康拓展开,hdu-2577dp)
    1-10假期训练(hdu-2059 简单dp)
    1-9-假期训练心得(dp+bfs)
    如何在IntelliJ Idea中同时启动不同端口的两个实例
    搭建Springboot监控中心报错A attempt was made to call the method reactor.retry.Retry.retryMax(I)Lreactor/ret)
    使用spring中遇到"java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor"问题
    mvc:annotation-driven的前缀 "mvc"未绑定
    使用Idea构建springmvc框架,出现no bean named 'cacheManager' is defined 错误
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/5051835.html
Copyright © 2011-2022 走看看