zoukankan      html  css  js  c++  java
  • (十五)关于继承

    ES5中继承的实现:
    //ES5中的思路是将子类的原型设置成为父类的原型
    function Super(){
    	Super.prototype.getNum = function(){
    		return 1;
    	}
    }
    function Sub(){
    
    }
    
    	let s = new Sub();
    	Sub.prototype = Object.create(Super.prototype,{
    		constructor:{
    			value:Sub,
    			writeable:true
    		}
    	})
    
    另外案例:
    // Shape - 父类(superclass)
    function Shape() {
      this.x = 0;
      this.y = 0;
    }
    
    // 父类的方法move
    Shape.prototype.move = function(x, y) {
      this.x += x;
      this.y += y;
      console.info('Shape moved.');
    };
    
    // Rectangle - 子类(subclass)
    function Rectangle() {
      Shape.call(this); // call super constructor.
    }
    
    // 子类续承父类;子类的prototype指向父类的prototype
    
    Rectangle.prototype = Object.create(Shape.prototype);
    Rectangle.prototype.constructor = Rectangle;
    
    //实例化子类对象
    var rect = new Rectangle();
    
    console.log('Is rect an instance of Rectangle?',
      rect instanceof Rectangle); // true
    console.log('Is rect an instance of Shape?',
      rect instanceof Shape); // true
    rect.move(1, 1); // Outputs, 'Shape moved.'
    
    Object.create()方法创建一个新对象,其作用是使用现有的对象来提供新创建的对象的__proto__,语法:

    Object.create(proto[, propertiesObject])

    ES6中直接用class … extends即可
    class MyDate extends Date(){
    	test(){
    		return this.getTime();	
    	}
    }
    let myDate = new MyDate();
    myDate.test();
    
  • 相关阅读:
    离线安装 Cloudera Manager 5 和 CDH5.10
    Sersync+Rsync实现触发式文件同步
    Azkaban3.x集群部署(multiple executor mode)
    内置函数
    递归
    嵌套函数,匿名函数,高阶函数
    局部变量,全局变量,作用域
    函数的介绍
    文件处理
    第二模块-三元运算
  • 原文地址:https://www.cnblogs.com/smileyqp/p/12675324.html
Copyright © 2011-2022 走看看