I am trying to wrap my head with pure functions, but I'm not sure that I really understand that. I know that pure functions should not mutate the external state, and it should return the same output every time it has the same input.
I know that, for example, this function is unclean because it mutates a basket variable that other parts of the program can use:
const addToCart = (cart, item) => {
cart.push(item);
return cart;
};
The same functions in their pure form:
const addToCart = (cart, item) => {
const newCart = lodash.cloneDeep(cart);
newCart.push(item);
return newCart;
};
That makes sense to me. I found out that pure functions should always return something.
However, I am working on some materials that require me to use the HTML5 canvas element, and I have this function that clears the canvas:
function clearCanvas(canvas) {
canvas.getContext('2d').clearRect(0, 0, canvas.width, canvas.height);
}
? , , , . DOM- ?
:)