Test task - closure

Run Settings
LanguageJavaScript
Language Version
Run Command
function combinedFunc(a = 0){ this.res = a; } combinedFunc.prototype.add = function(b){ this.res += b; return this; }; combinedFunc.prototype.deduct = function(b){ this.res -= b; return this; }; combinedFunc.prototype.mult = function(b){ this.res *= b; return this; }; combinedFunc.prototype.div = function(b){ this.res /= b; return this; }; combinedFunc.prototype.inc = function(){ this.res++; return this; }; combinedFunc.prototype.valueOf = function(){ return this.res; }; var cf = new combinedFunc(0); console.log(cf.add(6).mult(2).div(3).deduct(2).inc() == 3);
const scope = (val) => { return { add: add.bind(val), multiply: multiply.bind(val), divide: divide.bind(val), deduct: deduct.bind(val), inc: inc.bind(val), valueOf() { return val; } }; }; function add(val) { 'use strict'; val = (this || 0) + val; return scope(val); } function multiply(val) { 'use strict'; val = (this || 0) * val; return scope(val); } function divide(val) { 'use strict'; val = (this || 0) / val; return scope(val); } function inc(val) { 'use strict'; val = (this || 0)+1; return scope(val); } function deduct(val) { 'use strict'; val = (this || 0) - val; return scope(val); } console.log( add(6).multiply(2).divide(3).deduct(2).inc().deduct(0) == 3 );
Editor Settings
Theme
Key bindings
Full width
Lines