Skip to content

Instantly share code, notes, and snippets.

@bor0
Last active September 27, 2022 15:48
Show Gist options
  • Save bor0/ad90b5561e90c3d3bd9f11b8188996d8 to your computer and use it in GitHub Desktop.
Save bor0/ad90b5561e90c3d3bd9f11b8188996d8 to your computer and use it in GitHub Desktop.
function computation() {
this.red_tree = [];
this.valid = true;
this.add = function (x, y) {
if (!this.valid) return;
var res;
if (typeof x != 'number' || typeof y != 'number') {
this.valid = false;
res = 'error';
} else {
res = x+y;
}
this.red_tree.push( {
'op': 'add',
'x': x,
'y': y,
'res': res,
} );
this.res = res;
}
return this;
}
var foo = computation();
console.log(foo.red_tree);
console.log(foo.res);
foo.add(3,4);
console.log(foo.red_tree);
console.log(foo.res);
foo.add(3,'a');
console.log(foo.red_tree);
console.log(foo.res);
/*
[]
undefined
[ { op: 'add', x: 3, y: 4, res: 7 } ]
7
[
{ op: 'add', x: 3, y: 4, res: 7 },
{ op: 'add', x: 3, y: 'a', res: 'error' }
]
error
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment