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();
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());