classParent{constructor(name, age){this.name = name;this.age = age;this.run =this.run
}run(){return`${this.name } is running, age is ${this.age}`;}sleep(){return`${this.name } is sleeping, age is ${this.age}`;}}classChildextendsParent{constructor(name,age,run){super(name,age,run);//继承父的name,age属性和run方法}say(){
console.log(this.name,'is saying,age is',this.age)}}let child =newChild('zzz',12);
child.run()// "zzz is running, age is 12"
child.sleep()// "zzz is sleeping, age is 12"
console.log(child.constructor===Child);
console.log(child.__proto__.constructor===Child)// true
console.log(child.__proto__ ===Child.prototype)// true
console.log(child.__proto__.constructor.__proto__===Parent)//true
构造函数 通过原型链prototype
functionParent(name,age){this.name = name;this.age = age;this.run=function(){return`${this.name } is running, age is ${this.age}`;}this.sleep=function(){return`${this.name } is sleeping, age is ${this.age}`;}}functionChild(){this.say=function(){
console.log(this.name,'is saying,age is',this.age)}}
Child.prototype =newParent();let child =newChild('lisa',30);
child.say();// "lisa is saying,age is 30"
child.run();// "lisa is running, age is 30"
console.log(child.__proto__.constructor === Parent)// true
console.log(child.__proto__ === Child.prototype)// true
console.log(Child.prototype.constructor===Parent)// true//法二,通过call改变this的指向functionPhone(brand, price){this.brand = brand;this.price = price;this.callPhone=function(){
console.log(this.brand,this.price,'can call phone')}}functionSmartPhone(brand, price,color,size){
Phone.call(this,brand,price);this.color = color;this.size = size;}let smartPhone =newSmartPhone('小米',2999,'white','4.7');