Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the pure function of JavaScript

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces what the pure function of JavaScript is, it has certain reference value, interested friends can refer to it, I hope you can get a lot after reading this article, let the editor take you to understand it.

Pure function

A pure function is a function that does not depend on and does not change the state of variables outside its scope.

The return value of a pure function is determined only by the parameters when it is called, and its execution does not depend on the state of the system (execution context).

If you have the same input parameters, you will get the same output, that is, there are no random variables that will affect the output.

Characteristics that do not belong to pure functions:

Change the file system

Insert records into the database

Send a http request

Variable data

Print / log

Get user input

DOM query

Access system statu

The role of pure functions:

Reliability: function returns are always the same as expected

Cacheability: because as long as the input is the same, the output must be the same, so you can use the input as the key, the output as the value, and use the object to cache the calculated results

Portability: because there are no external dependencies, porting to any environment works correctly

Testability: make it easy to do unit tests for functions

Parallelism: for some complex computing, parallel computing can be done (for example, multiple tasks can be computed in parallel using multiple sub-processes of nodejs to improve computing speed)

Application scenarios:

It is best to use pure functions for tool functions.

Code used on multiple platforms (nodejs, browser, WeChat Mini Programs, native client, etc.)

Relatively independent function

Var a = 1return / function sum (b) {return a + b;} / / function sum (b) {a = 2; return b;} / function sum (b) {return b + Math.random ();} / / function sum (b, c) {return b + c } Thank you for reading this article carefully. I hope the article "what is the pure function of JavaScript" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report