[关闭]
@Secretmm 2017-12-09T07:43:16.000000Z 字数 2313 阅读 748

白板引申知识点

好好学习


Class

属性和方法

使用 class 定义类,使用 constructor 定义构造函数。

通过 new 生成新实例的时候,会自动调用构造函数。

  1. class Animal {
  2. constructor(name) {
  3. this.name = name;
  4. }
  5. sayHi() {
  6. return `My name is ${this.name}`;
  7. }
  8. }
  9. let a = new Animal('Jack');
  10. console.log(a.sayHi()); // My name is Jack

变量/方法的访问修饰符

static

static关键字,修饰的方法或属性不依赖于实例,可以直接通过类名来访问在类内部和外部访问方式一样;【多用于比较独立的变量或者方法--因为该类的实例是调用不了静态变量或者静态方法的】
eg:

  1. class Test() {
  2. static one: string;
  3. static getTest() {
  4. return 'test';
  5. }
  6. }
  7. //外面使用时
  8. let test = Test.one;
  9. let getTest = Test.getTest();

private

[看到说es6不存在私有属性和私有变量]?? 这里是ts,哈哈哈
修饰的属性或方法是私有的,不能在声明它的类的外部访问

public

修饰的属性或方法是公有的,可以在任何地方被访问到,默认所有的属性和方法都是 public

protected

protected 修饰的属性或方法是受保护的,它和 private 类似,区别是它在子类中也是允许被访问的


存取器

在“类”的内部可以使用getset关键字,对某个属性设置存值函数和取值函数,拦截该属性的存取行为。

这里的拦截是什么意思呢?还有使用场景是什么?
eg:

  1. class Platform {
  2. protected isAudioOn: boolean;
  3. constructor() {
  4. this.isAudioOn = false;
  5. }
  6. get audioOn() {
  7. return this.isAudioOn;
  8. }
  9. set audioOn(value) {
  10. this.isAudioOn = value;
  11. store.dispatch(value ? 'audioOpened' : 'audioClosed');
  12. }
  13. }

假如要实现一个功能,就是isAudioOn的值改变了之后,要执行store.dispatch(value ? 'audioOpened' : 'audioClosed'),可以这样做,设置isAuioOn为public属性,每当isAuioOn在改变的时候加上store.dispatch(value ? 'audioOpened' : 'audioClosed');
这样就很麻烦,因为store.dispath()要重复加。
此时就可以采用getset关键字来拦截isAudioOn的修改;

如上例:
isAudioOn包装成私有属性,外界通过设置audioOn的值,来修改isAudioOn的值;

  1. let plat = new Platform();
  2. plat.audioOn = true;

执行该语句时,被set拦截,实际上执行的是

  1. //value为true;
  2. set audioOn(value) {
  3. this.isAudioOn = value;
  4. store.dispatch(value ? 'audioOpened' : 'audioClosed');
  5. }

注意:
plat.audioOn = true;并不是在这里给audioOn赋值为true,实际上audioOn的值只等于get的返回值;在set中可以改变audioOn的值
eg:

  1. class MyClass {
  2. constructor() {
  3. }
  4. get prop() {
  5. return 'getter';
  6. }
  7. set prop(value) {
  8. console.log('setter');
  9. }
  10. }
  11. let inst = new MyClass();
  12. function test() {
  13. inst.prop = '666';//被拦截,执行的是set prop('666'),打印出'setter'
  14. console.log(inst.prop); // 'getter'
  15. }

类的继承

使用extends关键字实现继承,子类中使用super关键字来调用父类的构造函数和方法。

  1. class Animal {
  2. constructor(name) {
  3. this.name = name;
  4. }
  5. sayHi() {
  6. return `My name is ${this.name}`;
  7. }
  8. }
  9. let a = new Animal('Jack');
  10. console.log(a.sayHi()); // My name is Jack
  1. class Cat extends Animal {
  2. constructor(name) {
  3. super(name); // 调用父类的 constructor(name)
  4. console.log(this.name);
  5. }
  6. sayHi() {
  7. return 'Meow, ' + super.sayHi(); // 调用父类的 sayHi()
  8. }
  9. }
  10. let c = new Cat('Tom'); // Tom
  11. console.log(c.sayHi()); // Meow, My name is Tom

抽象类

abstract 用于定义抽象类和其中的抽象方法
- 抽象类不允许被实例化
- 抽象类中的抽象方法必须被子类实现

  1. abstract class Animal {
  2. public name;
  3. public constructor(name) {
  4. this.name = name;
  5. }
  6. public abstract sayHi();
  7. }
  8. class Cat extends Animal {
  9. public sayHi() {
  10. console.log(`Meow, My name is ${this.name}`);
  11. }
  12. }
  13. let cat = new Cat('Tom');
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注