0

Given I have a function called sum module imported from a 3rd party module Calculator. Here's how I use it:

Calculator.sum(1, 2) // returns 3  

I don't know the implementation of Calculator.sum. What if I want to modify the arguments passed to Calculator.sum before executing sum, so that instead of taking the original numbers (1, 2, 3, etc), sum will take their squared values (1, 4, 9, etc) ? For example:

Calculator.sum(1, 2) // returns 5
Calculator.sum(2, 3) // returns 13
Calculator.sum(3, 4) // returns 25

Note: This is just an example of my actual problem, I don't actually need a Calculator.sum function to calculate sum of squares 😀

3
  • Why would you want to modify the method instead of just passing the modified values into it? Its called sum. Just call the method with the modified values or create a new method for it. the Math "namespace" have all the functions you need for that. Commented Dec 11, 2017 at 8:25
  • It's just an example of my problem. Passing the modified values to it is straightforward but in my actual function, I have to modify a lot of existed unit tests Commented Dec 11, 2017 at 8:27
  • 1
    Yes, but if your unit tests are for a sum method that you expect to not actually sum you have either created unit tests for the wrong version or you might have to consider renaming a few things... I would recommend creating a new method in the Calculator object if that is what you feel you need, a method with a better name, that does what you wish. Not replace the sum method with something that is NOT sum :P Commented Dec 11, 2017 at 8:30

3 Answers 3

2

Couldn't you just store it under another name and overwrite it ?

Calculator.sum_stored = Calculator.sum;
Calculator.sum = function(a,b){
    return Calculator.sum_stored(Math.sqrt(a),Math.sqrt(b));
};

Then you can just call Calculator.sum as you wanted to.

Sign up to request clarification or add additional context in comments.

Comments

1

You could simply decorate the original function with you extra functionality, then call the original function with whatever values you wish.

If you are going to do something like this, you should really define a different function eg, calulator.squareAdd.

// this is your library object
const Calculator = {
  sum: (...args) => args.reduce((x, y) => x + y, 0)
}

console.log(
  'before decoration',
  Calculator.sum(1, 2)
)

// save a reference to the original function
const origSum = Calculator.sum
// decorate the sum function with your own function
Calculator.sum = function(...args) {
  return origSum.apply(Calculator, args.map(x => Math.pow(x, 2)))
}

// call the decorated function

console.log(
  'after decoration',
  Calculator.sum(1, 2), // returns 5
  Calculator.sum(2, 3), // returns 13
  Calculator.sum(3, 4) // returns 25
)
<script src="https://codepen.io/synthet1c/pen/KyQQmL.js"></script>

Comments

-1

You can modify the arguments in side the method like this ->

Calculator.sum(a,b) {
return a*a + b*b;
}

And if you want your method to be flexible then you can pass a third argument that will identify what operation needs to be performed on the arguments.

Calculator.sum(a,b,'square'){
    //use switch case here to return the correct result.
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.