Interview icon indicating copy to clipboard operation
Interview copied to clipboard

Day401:实现一个数字加减法功能(腾讯)

Open qappleh opened this issue 3 years ago • 1 comments

例如:

(5).add(3).minus(2)
5 + 3 - 2 = 6
3 - 1 + 2 = 4

qappleh avatar Sep 18 '21 03:09 qappleh

Object.defineProperties(Number.prototype, {
  add: { // 加
    value: function (num) { return this + num; }
  },
  sub: { // 减
    value: function (num) { return this - num; }
  }
});
(5).add(3).sub(2); // 6

如上,我只能想到直接扩展原生对象的方式。

Moushudyx avatar Sep 29 '21 06:09 Moushudyx