What is an example of an impure function in JavaScript

Having seen many pure functions and how they have no side effects, what will be an example of an unclean function that has always been antagonized as an unstable and main source of error?

+6
source share
6 answers

For example, an unclean function that has a side effect on a variable outside its own scope:

var count = 0; function increaseCount(val) { count += val; } 

Or a function that returns different values ​​for the same input, because it evaluates a variable that is not specified as a parameter:

 var count = 0; function getSomething() { return count > 0; } 
+11
source

A pure function is independent of and does not change the state of variables from the scope.

Specifically, this means that a pure function always returns the same result with the same parameters. Its implementation does not depend on the state of the system.

 var values = { a: 1 }; function impureFunction ( items ) { var b = 1; items.a = items.a * b + 2; return items.a; } var c = impureFunction( values ); // Now `values.a` is 3, the impure function modifies it. 

Here we change the attributes of this object. Therefore, we modify an object that lies outside the scope of our function: the function is unclean.

 var values = { a: 1 }; function pureFunction ( a ) { var b = 1; a = a * b + 2; return a; } var c = pureFunction( values.a ); 

we just change the parameter that is in the scope of the function, nothing changes outside!

 var values = { a: 1 }; var b = 1; function impureFunction ( a ) { a = a * b + 2; return a; } var c = impureFunction( values.a ); // Actually, the value of `c` will depend on the value of `b`. // In a bigger codebase, you may forget about that, which may // surprise you because the result can vary implicitly. 

Here b is not included in the scope of the function. The outcome will depend on the context: expected surprises!

 var values = { a: 1 }; var b = 1; function pureFunction ( a, c ) { a = a * c + 2; return a; } var c = pureFunction( values.a, b ); // Here it made clear that the value of `c` will depend on // the value of `b`. 

Link: For more information, click here.

+4
source

Math.random() is an unclean function; it changes the internal state of the Math object so that you get different values ​​in successive calls. console.log() and alert() are unclean functions because they have side effects (although they generate the same behavior and always return the same value for identical calls).

Any function that changes the internal state of one of its arguments or the value of an external variable is an unclean function.

How did you realize that unclean functions are always considered bad?

+2
source

An example I can come up with (it's really quite confusing) is Array#reverse() . Instead of returning a new array, it changes it in place when the original array is returned.

Other array functions, such as splice , push , shift , pop , unshift , are unshift .

0
source

pure function (get the argument and return the new value):

 function (objectArgument) { return { value: objectArgument.value + 123, // other arguments }; } 

impure function (get the argument, change it and return the changed object):

 function (objectArgument) { objectArgument.value += 123; return objectArgument; } 
0
source
  • The Impure functon function is a function that returns a different result for the same input parameters, that is, they depend on some state;
  • A function that can return the same result for the same input parameters, but that mutates a different state object . an object that it does not depend on, but others do // cause side effects.

Example:

1)

  var toggled = false; /* state */ /*impure function*/ function impureFn(number){ if(number%2 == 0) toggled = true; return toggled; } /*Execute this in order */ impureFn(5) // returns false impureFn(2) //returns true impureFn(5) // now returns true 
0
source

All Articles