//一些补充//在calss中,不管方法是否通过构造函数绑定到对象,区别在于://在构造函数中绑定方法会绑定在对象实例上//在构造函数外使用a(){}绑定的方法最终会绑定在实例对象的原型上//首先声明一个calss对象,calss实例的属性只能是写在constructor中,prototype属性可以另外加age}say(){//自包含方法console.log(`我的${this.name}是${this.age}岁的${this.id}`);}}//添加一个新方法Object.assign(Person.prototype,{dreams(){console.log(`就算我是${this.id},总有一天我会成为这片人海中最自由的人`);}})//添加方法Person.prototype.star=function(){console.log(`让我的名字${this.name}响彻世界`);}//创建对象实例letself=newPerson('张迪达',18)//调用测试self.say()self.dreams()self.star()//测试类的继承extends继承只是继承里面的方法,Data是notinteroperableclassOtherextendsPerson{name='大河川'age=66sayToo(){console.log(`I${this.name}isalso`);}}letanother=newOther()another.say()another.sayToo()
