0

How to program an operator on Javascript?

To make the sum of 2 object you must create an operator function, how to do it in Javascript?

19th May 2017, 9:16 AM
Mefoued Abdel Kader
Mefoued Abdel Kader - avatar
6 odpowiedzi
+ 19
operator overloading not supported in JavaScript....
19th May 2017, 9:54 AM
Valen.H. ~
Valen.H. ~ - avatar
+ 18
summ = []; function Sum(val) { summ.push(this); this.val = val; this.sum = function() { var sm = 0; summ.forEach(function(va) { sm += va.val; }); return sm; }//sum }//Sum new Sum(5); new Sum(6); alert(summ[1].sum());
19th May 2017, 9:39 AM
Valen.H. ~
Valen.H. ~ - avatar
+ 16
function sum(a,b) { return a+b; }//sum alert(sum(3,2));
19th May 2017, 9:21 AM
Valen.H. ~
Valen.H. ~ - avatar
+ 1
Thanks :(
19th May 2017, 9:56 AM
Mefoued Abdel Kader
Mefoued Abdel Kader - avatar
0
I know that. For example : I create class 'complex' then declare 2 complex objects z1 and z2. I want to be able to make the sum of this 2 object like this z3=z1+z2
19th May 2017, 9:35 AM
Mefoued Abdel Kader
Mefoued Abdel Kader - avatar
0
Is not what I'm looking for! This is example in c++ https://code.sololearn.com/c0A1ODBNCJRT/?ref=app
19th May 2017, 9:45 AM
Mefoued Abdel Kader
Mefoued Abdel Kader - avatar