"use strict"; classAnimal { constructor() { this.info = { age: 18, name: "unknown" }; //this.run(); } run() { console.log(" "); console.log("------------Animal run ------------------------------"); console.log(`Animal pre run ! age:${this.info.age} name:${this.info.name}`); this.info.name = "Animal类"; console.log(`Animal after run ! age:${this.info.age} name:${this.info.name}`); console.log(" "); } } classDogextendsAnimal { constructor() { super(); this.info = { age: 20, name: "Dog" }; console.log(`------重点是这个------ Dog2 run ! age:${this.info.age} name:${this.info.name}`); //console.log("----本来我以为这个应该是 name 是 哈士奇 结果是 子类默认值 Dog---") //this.run(); console.log(`Dog3 run ! age:${this.info.age} name:${this.info.name}`); } run() { console.log(" "); console.log("--------------Dog run----------------------------"); super.run(); console.log(`Dog pre run ! age:${this.info.age} name:${this.info.name}`); this.info.name = "哈士奇"; console.log(`Dog1 after run ! age:${this.info.age} name:${this.info.name}`); console.log(" "); } test() { console.log(" "); console.log("--------------Dog test----------------------------"); console.log(`Dog3 pre run ! age:${this.info.age} name:${this.info.name}`); this.info.name = "金毛"; console.log(`Dog4 after run ! age:${this.info.age} name:${this.info.name}`); } } let dog = newDog(); dog.run(); dog.test();
结果是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
"------重点是这个------ Dog2 run ! age:20 name:Dog" "Dog3 run ! age:20 name:Dog" " " "--------------Dog run----------------------------" " " "------------Animal run ------------------------------" "Animal pre run ! age:20 name:Dog" "Animal after run ! age:20 name:Animal类" " " "Dog pre run ! age:20 name:Animal类" "Dog1 after run ! age:20 name:哈士奇" " " " " "--------------Dog test----------------------------" "Dog3 pre run ! age:20 name:哈士奇" "Dog4 after run ! age:20 name:金毛"
"use strict"; classAnimal { constructor() { this.info = { age: 18, name: "unknown" }; this.run(); } run() { console.log("------------Animal run ------------------------------"); console.log(`Animal pre run ! age:${this.info.age} name:${this.info.name}`); this.info.name = "Animal类"; console.log(`Animal after run ! age:${this.info.age} name:${this.info.name}`); console.log(" "); } } classDogextendsAnimal { constructor() { super(); this.info = { age: 20, name: "Dog" }; console.log(`------重点是这个------ Dog2 run ! age:${this.info.age} name:${this.info.name}`); console.log("----本来我以为这个应该是 name 是 哈士奇 结果是 子类默认值 Dog---"); this.run(); console.log(`Dog3 run ! age:${this.info.age} name:${this.info.name}`); } run() { console.log("--------------Dog run----------------------------"); super.run(); console.log(`Dog pre run ! age:${this.info.age} name:${this.info.name}`); this.info.name = "哈士奇"; console.log(`Dog1 after run ! age:${this.info.age} name:${this.info.name}`); console.log(" "); } test() { console.log(" "); console.log("--------------Dog test----------------------------"); console.log(`Dog3 pre run ! age:${this.info.age} name:${this.info.name}`); this.info.name = "金毛"; console.log(`Dog4 after run ! age:${this.info.age} name:${this.info.name}`); } } let dog = newDog(); //dog.test()
输出结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
"--------------Dog run----------------------------" "------------Animal run ------------------------------" "Animal pre run ! age:18 name:unknown" "Animal after run ! age:18 name:Animal类" " " "Dog pre run ! age:18 name:Animal类" "Dog1 after run ! age:18 name:哈士奇" " " "------重点是这个------ Dog2 run ! age:20 name:Dog" "----本来我以为这个应该是 name 是 哈士奇 结果是 子类默认值 Dog---" "--------------Dog run----------------------------" "------------Animal run ------------------------------" "Animal pre run ! age:20 name:Dog" "Animal after run ! age:20 name:Animal类" " " "Dog pre run ! age:20 name:Animal类" "Dog1 after run ! age:20 name:哈士奇" " " "Dog3 run ! age:20 name:哈士奇"
"use strict"; classAnimal { constructor() { this.info = { age: 18, name: "unknown" }; this.run(); } run() { console.log("------------Animal run ------------------------------"); console.log(`Animal pre run ! age:${this.info.age} name:${this.info.name}`); this.info.name = "Animal类"; console.log(`Animal after run ! age:${this.info.age} name:${this.info.name}`); console.log(" "); } } classDogextendsAnimal { constructor() { super(); console.log(`------重点是这个------ Dog2 run ! age:${this.info.age} name:${this.info.name}`); console.log("----一些如我所愿 当下 name 是 哈士奇 子类默认值 Dog---"); this.run(); console.log(`Dog3 run ! age:${this.info.age} name:${this.info.name}`); } run() { console.log("--------------Dog run----------------------------"); super.run(); console.log(`Dog pre run ! age:${this.info.age} name:${this.info.name}`); this.info.name = "哈士奇"; console.log(`Dog1 after run ! age:${this.info.age} name:${this.info.name}`); console.log(" "); } test() { console.log(" "); console.log("--------------Dog test----------------------------"); console.log(`Dog3 pre run ! age:${this.info.age} name:${this.info.name}`); this.info.name = "金毛"; console.log(`Dog4 after run ! age:${this.info.age} name:${this.info.name}`); } } let dog = newDog(); //dog.test()
输出结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
"--------------Dog run----------------------------" "------------Animal run ------------------------------" "Animal pre run ! age:18 name:unknown" "Animal after run ! age:18 name:Animal类" " " "Dog pre run ! age:18 name:Animal类" "Dog1 after run ! age:18 name:哈士奇" " " "------重点是这个------ Dog2 run ! age:18 name:哈士奇" "----一些如我所愿 当下 name 是 哈士奇 子类默认值 Dog---" "--------------Dog run----------------------------" "------------Animal run ------------------------------" "Animal pre run ! age:18 name:哈士奇" "Animal after run ! age:18 name:Animal类" " " "Dog pre run ! age:18 name:Animal类" "Dog1 after run ! age:18 name:哈士奇" " " "Dog3 run ! age:18 name:哈士奇"