qappleh / Interview

我是追梦赤子心,公众号「深圳湾码农」的作者,某上市集团公司高级前端开发,深耕前端领域多年,每天攻破一道题,带你从0到1系统构建web全栈完整的知识体系!
https://github.com/qappleh/Interview
1.14k stars 95 forks source link

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

Open qappleh opened 2 years ago

qappleh commented 2 years ago

例如:

(5).add(3).minus(2)
5 + 3 - 2 = 6
3 - 1 + 2 = 4
Moushudyx commented 2 years ago
Object.defineProperties(Number.prototype, {
  add: { // 加
    value: function (num) { return this + num; }
  },
  sub: { // 减
    value: function (num) { return this - num; }
  }
});
(5).add(3).sub(2); // 6

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