Created
September 25, 2018 06:57
-
-
Save guimeisang/456d85ec7d30e97fe17766e2f9a4891a to your computer and use it in GitHub Desktop.
Calculator [add your bin description] // source https://jsbin.com/wituqen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="description" content="[add your bin description]"> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>Calculator</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
/* | |
请实现一个加减乘除的计算器,如下调用方式: | |
new Calculator(10).add(2).add(3).multiply(3).multiply(1).print(); // 打印 45 | |
*/ | |
"use strict"; | |
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); | |
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | |
var Calculator = (function () { | |
function Calculator(initNum) { | |
_classCallCheck(this, Calculator); | |
this.result = initNum || 0; | |
} | |
_createClass(Calculator, [{ | |
key: "add", | |
value: function add(num) { | |
this.result += num; | |
return this; | |
} | |
}, { | |
key: "reduce", | |
value: function reduce(num) { | |
this.result -= num; | |
return this; | |
} | |
}, { | |
key: "multiply", | |
value: function multiply(num) { | |
this.result *= num; | |
return this; | |
} | |
}, { | |
key: "divide", | |
value: function divide(num) { | |
this.result /= num; | |
return this; | |
} | |
}, { | |
key: "print", | |
value: function print() { | |
console.log(this.result); | |
return this; | |
} | |
}]); | |
return Calculator; | |
})(); | |
new Calculator(10).add(2).add(3).multiply(3).multiply(1).print(); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">/* | |
请实现一个加减乘除的计算器,如下调用方式: | |
new Calculator(10).add(2).add(3).multiply(3).multiply(1).print(); // 打印 45 | |
*/ | |
class Calculator{ | |
constructor(initNum){ | |
this.result = initNum || 0; | |
} | |
add(num){ | |
this.result += num; | |
return this; | |
} | |
reduce(num){ | |
this.result -= num; | |
return this; | |
} | |
multiply(num){ | |
this.result *= num; | |
return this; | |
} | |
divide(num){ | |
this.result /= num; | |
return this; | |
} | |
print(){ | |
console.log(this.result); | |
return this; | |
} | |
} | |
new Calculator(10).add(2).add(3).multiply(3).multiply(1).print(); | |
</script></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
请实现一个加减乘除的计算器,如下调用方式: | |
new Calculator(10).add(2).add(3).multiply(3).multiply(1).print(); // 打印 45 | |
*/ | |
"use strict"; | |
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); | |
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | |
var Calculator = (function () { | |
function Calculator(initNum) { | |
_classCallCheck(this, Calculator); | |
this.result = initNum || 0; | |
} | |
_createClass(Calculator, [{ | |
key: "add", | |
value: function add(num) { | |
this.result += num; | |
return this; | |
} | |
}, { | |
key: "reduce", | |
value: function reduce(num) { | |
this.result -= num; | |
return this; | |
} | |
}, { | |
key: "multiply", | |
value: function multiply(num) { | |
this.result *= num; | |
return this; | |
} | |
}, { | |
key: "divide", | |
value: function divide(num) { | |
this.result /= num; | |
return this; | |
} | |
}, { | |
key: "print", | |
value: function print() { | |
console.log(this.result); | |
return this; | |
} | |
}]); | |
return Calculator; | |
})(); | |
new Calculator(10).add(2).add(3).multiply(3).multiply(1).print(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment