-
-
Notifications
You must be signed in to change notification settings - Fork 235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
实现(5).add(3).minus(2)功能 #115
Comments
(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 |
Number.prototype.add = function(num) {
return this + (+num);
}
Number.prototype.minus = function(num){
return this - (+num);
}
console.log((5).add(3).minus(2)); |
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;
} |
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 |
|
1 similar comment
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No description provided.
The text was updated successfully, but these errors were encountered: