Problem: 2665. 计数器 II
1234567891011121314151617181920212223242526272829
/** * @param {integer} init * @return { increment: Function, decrement: Function, reset: Function } */var createCounter = function(init) { return{ initialVal:init, value:init, increment: function () { this.value++ return this.value }, decrement: function (){ this.value-- return this.value }, reset: function (){ this.value=this.initialVal return this.value } }};/** * const counter = createCounter(5) * counter.increment(); // 6 * counter.reset(); // 5 * counter.decrement(); // 4 */
123456789101112131415161718192021222324252627
/** * @param {integer} init * @return { increment: Function, decrement: Function, reset: Function } */var createCounter = function (init) { const ret = { initialVal: init, value: init, increment: function () { return ++this.value }, decrement: function () { return --this.value }, reset: function () { return this.value = this.initialVal } } return ret;};/** * const counter = createCounter(5) * counter.increment(); // 6 * counter.reset(); // 5 * counter.decrement(); // 4 */