Yubin Qiu Yubin Qiu - 5 months ago 20
jQuery Question

javascript passing value using function

I have two js file named one.js and two.js (both imported)

I want to pass a value from two to one upon triggered.

Example would be

one.js

var a = 1;
var b = 2;

var calc = new calculator();

var getResult = calc.add(a, b);

console.log(getResult);


two.js

this['calculator'] = (function() {

function add(a, b) {
return a+b;
}
}


When I run one.js, it returns me a 3 from two.js line 4.

However, how do pass this value '3' back so i could print it in one.js?

thanks

Answer

Maybe I misunderstood, but something like this should do the trick :

One.js

var a = 1;
var b = 2;
var calc = new calculator();
console.log(calc.add(a, b));

Two.js

this['calculator'] = (function() {
      function add(a, b) {
           return a+b;
      }
}