本文介绍了Javascript闭包和副作用在简单的英语? (分别)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在读一些JavaScript书,我总是听到关闭和副作用。由于某种原因,我不明白他们真的是什么。任何人都可以向我解释他们在普通英语中加什么例子?

I've been reading some JavaScript books and I always hear about closures and side effects. For some reason I can't understand what they really are. Can anyone explain to me what they are in plain English plus examples? (as you were explaining it to someone with the programming level of a graphic designer).

推荐答案

副作用是更简单的概念。 纯函数是将其输入值映射到输出值 def plus(x,y){return x + y; } 。 副作用是除了返回值之外的任何效果。例如:

Side effects are the easier concept. A "pure function" is a function that maps its input value(s) into an output value def plus(x, y){ return x + y; }. A "side effect" is any effect other than that return value. So, for instance:

def plusWithSideEffects(x, y) { alert("This is a side effect"); return x + y; } 

具有提高警告对话框(需要用户交互)的副作用。每个代码函数都有一些副作用(它们都消耗内存,需要时间,如果没有其他),但是当人们谈论副作用时,他们通常最关心的是IO(像上面的警报对话框)或者写入超出函数执行周期的状态。

has the side effect of raising an alert dialog (and requiring user interaction). Every code function has some side effects (they all consume memory and take time, if nothing else), but when people talk about side effects, they are often most concerned with either IO (like the alert dialog above) or the writing of state that lives beyond the execution period of the function.

副作用的挑战是,它们使得函数难以推理和重用。 (更容易推理和重用尽可能接近纯函数的函数,因为他们倾向于做一件好事。)

The challenge with side effects is that they make functions harder to reason about and to reuse. (It's much easier to reason and reuse functions that are as close to "pure functions" as possible, since they tend to "do one thing well.")

这篇关于Javascript闭包和副作用在简单的英语? (分别)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 01:49