ES5 构造函数
function Person(){ this.name='建林'; this.age=18; this.say=function(){ console.log('person的say') } } let p1=new Person(); console.log(p1.name); p1.say();View Code
ES6 构造函数与继承
//ES6语法 class class Person { constructor() { this.name = "建林"; this.age = 18; } say() { console.log('person的say') } } let p2 = new Person(); console.log(p2.name); p2.say(); //ES6构造函数继承 class Child extends Person{ //复杂写法 // constructor(){ // //继承必须加super() // super(); // this.sex="男"; // this.name="思聪";//继承覆盖了 // this.score=1000; // } //简单写法 sex='男'; name='思聪'; score=1000; hello(){ console.log('hello'); } abc(){ console.log(abc); } } let p3=new Child(); console.log(p3); console.log(p3.name); console.log(p3.say()); console.log(p3.hello());View Code
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。