es6中的类中的静态方法以及继承

在类中的实例属性前添加static关键字,不能被实例继承,不在实例对象和原型对象上,只能通过 类名.属性名 调用;

在类中的方法添加static关键字,不能被实例继承,不在实例对象和原型对象上,只能通过 类名.方法名 调用。

 

继承

 1 <script>
 2     class Person {
 3         constructor(name, age) {
 4             this.name = name;
 5             this.age = age;
 6         }
 7 
 8         run() {
 9             console.log(`已经${this.age}的${this.name}正在running`)
10         }
11     }
12 
13     class XiaoMing extends Person {
14         constructor(name, age) {
15             super(name, age)
16             this.name = name;
17             this.age = age
18         }
19 
20         jump() {
21             console.log('jumping')
22         }
23     }
24 
25     //xm.__proto__ === Person.prototype  //false
26     //xm.__proto__ === XiaoMing.prototype  //true
27     const xm = new XiaoMing('zhangsan', 20);
28     console.log(xm)
29 
30     console.log(XiaoMing.prototype)
31     // 输出结果,相当于class XiaoMing中的XiaoMing就是一个构造函数,构造函数的prototype指向函数的原型对象,对象包含的键值对就是在class XiaoMing中定义的所有方法
32     /*constructor: class XiaoMing
33     [[Prototype]]: Object*/
34 
35     //xm.__proto__ === XiaoMing.prototype   //true
36     //XiaoMing.prototype.__proto__ === Person.prototype     //true
37     //extends相当于实现了 xm.__proto__ === XiaoMing.prototype ;  XiaoMing.prototype.__proto__ === Person.prototype
38 </script>

 

posted @ 2021-10-11 16:47  亦茫茫  阅读(295)  评论(0)    收藏  举报