javascript全栈开发实践-web-10

死程序不说谎 在webpack部分最后,我们用断言assert代替之前的throw Error。 首先,我们在之前的代码ActionData里面:

export default class ActionData {
  //
  constructor(type, lineWidth, strokeStyle) {
    this._type = type;
    this._lineWidth = lineWidth;
    this._strokeStyle = strokeStyle;
  }
  //
  draw(ctx) { throw new Error('draw not implemented'); }
}
复制代码

draw方法实现里面,我们直接抛出了一个异常。这表明,如果不应该运行到这里。因为这个方法,是需要子类去实现的。如果子类没有实现,就会出现这个异常。 在这种情况下,对于出现了我们认为不应该出现的情况,我们应该尽快让程序停止运行,而不应该忽略这种错误,让程序错误的运行下去。否则的话,我们可能会很久都无法发现这个错误,也有可能会导致错误要花很长事件才能找到原因。 在之前,我们直接抛出异常。现在我们可以换用更好的方式:assert。

import assert from 'assert';

export default class ActionData {
  //
  constructor(type, lineWidth, strokeStyle) {
    this._type = type;
    this._lineWidth = lineWidth;
    this._strokeStyle = strokeStyle;
  }
  //
  draw(ctx) { assert(false, 'draw not implemented'); }
}

复制代码

从下次开始,我们将开始使用react来改造我们的程序,让我们的程序更加漂亮。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值