-
-
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
请实现一个模块 math,支持链式调用math.add(2,4).minus(3).times(2); #135
Comments
class math {
constructor(initValue = 0) {
this.value = initValue;
}
add(...args) {
this.value = args.reduce((pre, cur) => pre + cur, this.value);
return this;
}
minus(...args) {
this.value = this.value - args.reduce((pre, cur) => pre + cur);
return this;
}
times(timer) {
this.value = timer * this.value;
return this;
}
getVal() {
return this.value;
}
} |
class math { |
const math = {
result: 0,
add: function (...args) {
this.result += args.reduce((pre, cur) => pre + cur, this.result);
return this;
},
minus: function (value) {
this.result -= value;
return this;
},
times: function (value) {
this.result *= value;
return this;
},
getResult: function () {
return this.result;
}
};
// test code
const result = math.add(2, 4).minus(3).times(2).getResult();
console.log(result); // 输出: 6 |
这个题有两种想法:
|
|
|
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: