(精华)2020年7月4日 JavaScript高级篇 ES6(class类)

网友投稿 562 2022-05-30

是什么

es6新提供的一种生成对象实例的写法 es5构造函数的另外一种写法(语法糖)

作用

无疑让写法更清晰 更像面向对象的写法

回顾之前es5生成实例对象

Person.prototype.say = function(){ console.log('我会说话'); } // 这是错误的写法 // Person.prototype.say = ()=>{ // console.log(`我是${this.name}`); // } function Person(name,age){ this.name = name this.age = age } let person1 = new Person('张三',18) person1.say()

1

2

3

4

5

6

7

8

9

10

11

(精华)2020年7月4日 JavaScript高级篇 ES6(class类)

12

13

14

class类生成实例对象

class Person{ constructor(name,age){ this.name = name this.age = age } say(){ console.log(`我是${this.name}`); } } let person2 = new Person('李四',20) person2.say() console.log(typeof Person); // function console.log(person2.constructor === Person);

1

2

3

4

5

6

7

8

9

10

11

12

13

constructor 方法

// es6类的方法都定义在类的prototype属性上面 // 其实调用es6类的方法就是调用原型上的方法 class Car{ constructor(){ console.log(this); // 指向其他的对象 null return Object.create(null) } } let bmw = new Car() console.log(bmw instanceof Car)

1

2

3

4

5

6

7

8

9

10

11

12

共享一个原型对象

class Person{ constructor(name) { this.name = name } } Person.prototype.get = function(){ console.log('我是父类') } let person1 = new Person('张三') let person2 = new Person('李四') person1.get() person2.get()

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

取值函数和存值函数

const person = { get name(){ console.log(name); return name }, set name(value){ console.log(value); name = value } } person.name = 'tony' // console.log(); console.log(person.name); class MyClass{ get prop(){ return MyClass.prop } set prop(value){ MyClass.prop = value } } let init = new MyClass() init.prop = 123 console.log(init.prop);

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

this指向问题

// 第一种 箭头函数 eat = ()=>{ this.get() } // 第二种 绑定this constructor(){ this.eat = this.eat.bind(this) }

1

2

3

4

5

6

7

8

实例属性的新写法

class Person{ name = 'tony' }

1

2

3

静态属性

// 静态属性 指的是class本身的属性 Class.propname 并不是实例的属性 class Person{ static age = 18 // 现在静态属性的写法 name = 'tony' } let person1 = new Person() console.log(person1.age);// undefined

1

2

3

4

5

6

7

8

静态方法

// 静态方法 class Person{ static age = 18 // 现在静态属性的写法 static say(){ console.log('hello'); } name = 'tony' } // let person1 = new Person() Person.say()

1

2

3

4

5

6

7

8

9

10

私有属性

以前用闭包实现私有属性

class Math{ #count = 0 add(){ this.#count ++; return this.#count } } let math = new Math() // console.log(math.#count); 会报错 console.log(math.add());

1

2

3

4

5

6

7

8

9

10

私有方法

class MathAdd{ #a; #b; constructor(){ this.#a = 1; this.#b = 2; } #sum = ()=>{ return this.#a + this.#b } conSum(){ console.log(this.#sum()); } } let mathadd = new MathAdd() // console.log(math.#count); 会报错 mathadd.conSum()

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

Elasticsearch JavaScript

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Redis 应用场景
下一篇:软考——操作系统知识之存储管理
相关文章