温习日志
——2023年2月14日下午
学习内容
- Setters and Getters
- 在
class中设置,set和get
set 函数名(必须有一个参数) {}是当调用该函数名时,处理该参数
get 函数名() {}是当调用该函数名时,会返回处理好的字段
- 对于
set和get设置内部的属性,有一个约定就是在属性前加下划线_属性名
- Static Methods
- 在类中,添加方法在前面添加
static,则称为该类独有的方法,只能通过该类引用,不在原型链上,所以不会被继承
- Object.create()
- 可以自定义对象的
prototype,手动创建,如: const steven = Object.create(手动设置的prototype),则steven.__proto__ === 手动设置的prototype,steven为空对象
- 练习2,详见于代码
- Inheritance Between _Classes__Constructor Functions
- 通过创建构造函数
Person和Student,然后我们可以通过Student.prototype = Object.create(Person.prototype)实现继承
- 通过访问
Student.prototype.constructor会访问到Person,可以通过Student.prototype.constructor = Student实现
- 练习3, 详见于代码
- Inheritance Between _Classes__ES6 Classes
- 通过使用
extends关键字,可以直接实现两个类的继承,如: class Student extends Person
- 通过
extends继承后,Student类的构造函数,对于他父类构造函数相同的参数,要使用super(相同参数)后,再将不同的通过this赋值
- 类中的公共方法都是会被添加到原型链上的
- 只有使用
static才指定只能通过引用该类使用
- Inheritance Between _Classes__Object.create
- 通过创建
class PersonProto,然后将const StudentProto = Object.create(PersonProto)实现PersonProto成为StudentProto原型链
- 在
StudentProto添加init方法,对于重复的部分,可以引用PersonProto中相同的方法,如:PersonProto.init.call(this)
- Another Class Example
- Encapsulation_Protected Properties and Methods
- 对于类中的公共字段,可以直接
name = 'su'直接赋值,可以不要通过构造函数赋值
- 对于私有字段,只能通过类内部访问,通过
#关键字使用,在构造函数中this.#pin = pin,在外部是查询不到的
- 私有字段需要在类中提前声明,
#pin;
- Chaining
- 链式写法,在类方法中,通过最终返回
this实现
- 练习4,详见于代码
posted @
2023-02-14 16:33
这样那样如此如此
阅读(
23)
评论()
收藏
举报