首页 > 其他分享 >温习日志-17

温习日志-17

时间:2023-02-14 16:45:47浏览次数:32  
标签:PersonProto 17 create Object Student 温习 日志 prototype 构造函数

温习日志

——2023年2月14日下午

学习内容

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

标签:PersonProto,17,create,Object,Student,温习,日志,prototype,构造函数
From: https://www.cnblogs.com/jsst/p/17120067.html

相关文章