ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
# class类的继承结构 ES6中使用class定义类只是一种语法糖,底层最终还是转换成ES5中使用的function类定义类,及其其中的实例成员 和原型成员。 <br> ``` class Person { constructor(name,age){ this.name = name; this.age = age; } // 实例上的方法 static say(){ console.log("世界,你好"); } // 原型上的方法 sayHi(){ console.log("是的,你好"); } } class Animal extends Person{//Animal 继承Person类 constructor(name,age){ super(name,age);//传参 ,使用super关键字调用父类中的构造方法。 } // 实例上的方法 static eat(){ console.log("麻辣烫"); } // 原型上的方法 play(){ console.log("睡觉"); } } // 实例化对象 var dog = new Animal("wc",3); // 获取dog的属性 console.log(dog.name); console.log(dog.age); // 获取dog原型上的方法 dog.play(); dog.sayHi(); // 获取dog上的方法 Animal.say(); Animal.eat(); ```