Interview
Interview copied to clipboard
Day401:实现一个数字加减法功能(腾讯)
例如:
(5).add(3).minus(2)
5 + 3 - 2 = 6
3 - 1 + 2 = 4
Object.defineProperties(Number.prototype, {
add: { // 加
value: function (num) { return this + num; }
},
sub: { // 减
value: function (num) { return this - num; }
}
});
(5).add(3).sub(2); // 6
如上,我只能想到直接扩展原生对象的方式。