实现(5).add(3).minus(2)功能
Sunny-117 opened this issue · comments
Sunny commented
xiaoy commented
(function() {
function check(x) { //统一转成数字
return x = isNaN(x) ? 0 : x - 0
}
function add (x) {
x = check(x)
return this + x
}
function minus (x) {
x = check(x)
return this - x
}
Number.prototype.add = add
Number.prototype.minus = minus
})();
console.log((5).add('12').minus('1')) // 16
console.log((5).add(2)) // 7
console.log((5).add('abc').minus('1')) // 4
beary commented
Number.prototype.add = function(num) {
return this + (+num);
}
Number.prototype.minus = function(num){
return this - (+num);
}
console.log((5).add(3).minus(2));
veneno_o commented
Number.prototype.add = function(...numbs){
let res = this;
for (const num of numbs) {
res += (~~num);
}
return res;
}
Number.prototype.minus = function(...numbs){
let res = this;
for (const num of numbs) {
res = Math.min(this, (~~num));
}
return res;
}
kangkang123269 commented
Number.prototype.add = function(num) {
return this.valueOf() + num;
};
Number.prototype.minus = function(num) {
return this.valueOf() - num;
};
console.log((5).add(3).minus(2)); // 输出 6