ES6类的封装继承

ES6有了class类的概念,开发过程中使得封装更加方便

 class Animal {
      constructor() {
           this.eyes = 'eyes';
           this.hair = 'hair';
       }
       run() {
           console.log('Animal can run!');
       }
 }

这个Animal类声明了属性和方法

class Cat extends Animal {
	        constructor() {
	            super();
	            this.mouse = mouse;
	        }
	        eat() {
	            console.log('cat eat fish!');
	        }
}

这个猫类继承了Animal类,并添加了自己的属性和方法

本质是上ES6的类是对ES5类的包装而已,通过typeof Animal,可以发现Annimal类的类型时候function,实质上就是一种构造函数

猜你喜欢

转载自blog.csdn.net/weixin_39675478/article/details/87343762