[: currentTime | date:'mm:ss' :] [: timeLeft | date:'mm:ss' :]

示例参考 - ES5

<script>
  /**
   * 支付方案
   * @param balance 余额
   * @constructor
   */
  function Payment(balance) {
    this.balance = balance;
    this.next = null;
  }

  /**
   * 设置下一个责任节点(下一个工位)
   * @param {Payment} payment
   * @return {*}
   */
  Payment.prototype.setNext = function (payment) {
    return this.next = payment;
  }

  /**
   * 是否可以支付
   * @param {number} amount
   * @return {boolean}
   */
  Payment.prototype.canPay = function (amount) {
    return this.balance >= amount;
  }

  /**
   * 支付
   * @param {number} amount 支付的数额
   * @return {boolean}
   */
  Payment.prototype.pay = function (amount) {
    // 缓存更改前的余额
    var oldBalance = this.balance;

    // 先将数额减去
    this.balance -= amount;

    // 如果当前账户余额不足,就尝试下一个账户
    if (this.balance < 0) {
      // 如果下一个账户存在且支付成功(余额充足)
      if (this.next && this.next.pay(-this.balance)) {
        // 清空当前账号余额
        this.balance = 0;
      } else {
        // 由于下一个账号支付失败,还原当前账号余额
        this.balance = oldBalance;
        return false;
      }
    }

    // 能执行到这一步就说明一切正常,支付成功
    return true;
  }

  var amount = 100;
  var a = new Payment(1);
  var b = new Payment(2);
  var c = new Payment(3);

  a
    .setNext(b)
    .setNext(c);

  var result = a.pay(amount);

  console.log(result);
  console.log(a.balance, b.balance, c.balance);
</script>

示例参考 - ES6

<script>
  class Payment {
    /**
     * 支付方案
     * @param balance 余额
     * @constructor
     */
    constructor(balance) {
      this.balance = balance;
      this.next = null;
    }

    /**
     * 设置下一个责任节点(下一个工位)
     * @param {Payment} payment
     * @return {*}
     */
    setNext(payment) {
      return this.next = payment;
    }

    /**
     * 是否可以支付
     * @param {number} amount
     * @return {boolean}
     */
    canPay(amount) {
      return this.balance >= amount;
    }

    /**
     * 支付
     * @param {number} amount 支付的数额
     * @return {boolean}
     */
    pay(amount) {
      // 缓存更改前的余额
      var oldBalance = this.balance;

      // 先将数额减去
      this.balance -= amount;

      // 如果当前账户余额不足,就尝试下一个账户
      if (this.balance < 0) {
        // 如果下一个账户存在且支付成功(余额充足)
        if (this.next && this.next.pay(-this.balance)) {
          // 清空当前账号余额
          this.balance = 0;
        } else {
          // 由于下一个账号支付失败,还原当前账号余额
          this.balance = oldBalance;
          return false;
        }
      }

      // 能执行到这一步就说明一切正常,支付成功
      return true;
    }
  }

  var amount = 2;
  var a = new Payment(1);
  var b = new Payment(2);
  var c = new Payment(3);

  a
    .setNext(b)
    .setNext(c);

  var result = a.pay(amount);

  console.log(result);
  console.log(a.balance, b.balance, c.balance);
</script>
登录后评论