官术网_书友最值得收藏!

Pure functions

The most common definition of a pure function is a function that does not have side effects. This is to say that the returned value of a pure function is not affected, influenced, or changed by anything other than its input parameters. Provided the same input, the pure function always generates the same output. An example is as follows:

const sum = (a, b) => a + b;

This is an example of a pure function. Assuming that you call the function sum(6,9), the result is always 15, irrespective of the number of times that you run the function. You can be confident, if you are calling a pure function with the same input, that you are always going to get the same output, which means that the output is predictable. An example of an impure function is the following square function. In addition to returning the square of the number, the function might be updating the number in a database:

function square(number) {
updateNumberInDB(number);
return number * number;
}
主站蜘蛛池模板: 新丰县| 始兴县| 仁怀市| 太和县| 钟山县| 明星| 广东省| 滨州市| 福清市| 库尔勒市| 湘阴县| 门源| 武陟县| 海门市| 惠水县| 梁山县| 嘉峪关市| 和平县| 卢龙县| 南昌县| 苍南县| 来宾市| 钦州市| 灵武市| 阿拉善左旗| 阿图什市| 三原县| 佳木斯市| 嘉鱼县| 贞丰县| 乳源| 健康| 尚义县| 冀州市| 越西县| 海盐县| 黎城县| 馆陶县| 绵阳市| 湟源县| 奉节县|