-
Notifications
You must be signed in to change notification settings - Fork 5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
利用“Immutability(不可变性)”编写更为简洁高效的代码 #1688
Conversation
校对认领~ @sqrthree |
@bambooom 好哒~ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
> * 校对者: | ||
|
||
# Write safer and cleaner code by leveraging the power of “Immutability” # | ||
# 利用“Immutability(不可变性)”编写更为简洁高效的代码 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
“ ”
是英文标点,改成「」或者去掉?
|
||
Immutability is one of the building blocks of functional programming. It allows you to write safer and cleaner code. I’ll show you how you can achieve immutability through some JavaScript examples. | ||
不可变性是函数式编程中的一部分,它允许你写更安全和简洁的代码。我将会通过一些 JavaScript 的例子来告诉你如何达到不可变性。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
它允许你写更安全和简洁的代码 -> 它可以使你写出更安全更简洁的代码
|
||
> An immutable object (unchangeable object) is an object whose state cannot be modified after it is created. This is in contrast to a mutable object (changeable object), which can be modified after it is created. In some cases, an object is considered immutable even if some internally used attributes change but the object’s state appears to be unchanging from an external point of view. | ||
> 一个不可变对象(不能被改变的对象)是指在创建之后其状态不能被更改的对象,这与在创建之后可以被更改的可变对象(可以被改变的对象)相反。在某些情况下,一个对象的外部状态如果从外部看来是未不变的,那么即使它的一些内部属性更改了,仍被视为不可变对象。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
一个对象的外部状态如果从外部看来是未不变的 -> 一个对象的状态如果从外部看来没有变化
|
||
Arrays are a good starting point to get a grasp of how immutability actually works. Lets take a look. | ||
数组是抓住不可变性如何工作的一个要点。我们来看一下。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-> 数组是了解不可变性如何运作的一个很好的起点
|
||
Remember: When using [push](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push) to add a value to an array, you are **mutating** the array. You want to avoid mutating variables because it can cause side effects in your code. The [slice](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice) function returns a copy of the array. | ||
记住:当使用 [push](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push) 来给数组添加一个值时,你在**改变**这个数组,你想要避免值的改变因为这个可能会影响你代码里的其他部分。[slice](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice) 会返回一个复制的数组。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
你想要避免值的改变因为这个可能会影响你代码里的其他部分 -》 因为这样可能会影响代码里的其他部分,所以你想要避免使变量发生变异
(调整语序,更通顺一点)
@@ -72,11 +72,11 @@ console.log(add(array, 4)); // [1, 2, 3, 4] | |||
console.log(add(array, 5)); // [1, 2, 3, 4, 5] | |||
``` | |||
|
|||
So again, we are **mutating** our input which creates an unpredictable function. In the functional programming world, there is a golden rule around functions: **a function with the same input should always return the same result**. | |||
所以再一次的,我们**改变**我们的输入来创建一个不可预测的函数。在函数式编程的世界里,有一个关于函数的铁律:**函数对于相同的输入应当返回相同的值。** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我们改变我们的输入来创建一个不可预测的函数 -> 我们改变了输入的变量的值,这使得这个函数变得不可预测。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
所以再一次的,我们改变我们的输入来创建一个不可预测的函数。
=>
于是我们又一次改变了输入的变量的值,这使得这个函数变得不可预测。
@@ -99,37 +99,38 @@ const resultB = add(array, 5); | |||
console.log(resultB); // [1, 2, 3, 5] | |||
``` | |||
|
|||
Now we can call our function multiple times, and expect the output to be the same, based on the input. This is because we are no longer mutating the **array** variable. We can call this function a “pure function”. | |||
现在我们可以多次调用这个函数,然后根据相同的输入,获得相同的输出。这是因为我们不再改变 **array** 变量。我们把这个函数叫做“纯函数”。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
然后根据相同的输入,获得相同的输出 -> 且相同的输入获得相同的输出,与预期一致
|
||
NodeJS applications use a concept called concurrency. A concurrent operation means that two computations can both make progress regardless of the other. If there are two threads, the second computation doesn’t need to wait for the completion of the first one in order to advance. | ||
NodeJS 的应用有一个叫并发的概念,并发的操作是指两个计算可以同时的进行而不用管另外的一个。如果有两个线程,第二个的计算不需要等待第一个的完成。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- -> 并发操作是指两个计算可以同时进行不用管另外一个 (去掉不必要的
的
) - -> 第二个计算不需要等待第一个完成才能继续 (去掉不必要的
的
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
第二个计算不需要等待第一个完成才能继续
=>
第二个计算不需要等待第一个完成才能开始
继续 改成 开始 如何?
|
||
NodeJS makes concurrency possible with the event-loop. The event-loop repeatedly takes an event and fires any event handlers listening to that event one at a time. This model allows a NodeJS application to process a huge amount of requests. If you want to learn more, read [this article about the event-loop](https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick). | ||
NodeJS 用事件循环机制使并发成为可能。事件循环循环重复接收事件,并对每个事件添加监听。这个模型允许 NodeJS 的应用处理大规模的请求。如果你想学习更多,读一下[这篇关于事件循环的文章](https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick)。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 事件循环循环重复接收事件 -> 事件循环重复接收事件 (多了一个
循环
) - 并对每个事件添加监听 -> 并一次触发一个监听该事件的处理程序
|
||
What does immutability have to do with concurrency? Since multiple operations can change a value outside of the function’s scope in a concurrent way, this creates unreliable output and causes unexpected results. Be aware of a function that mutates variables outside of its scope, as this can be really dangerous. | ||
不可变性跟并发又有什么关系呢?由于多个操作可能并发的改变函数的作用域的值,这将会产生不可靠的输出和导致意向不到的结果。明确函数是否改变它作用域之外的值,因为这可能真的会很危险。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 意向不到 -> 意想不到 (错别字)
- 明确 -> 注意
认领校对~~ @sqrthree =w= |
@xunge0613 妥妥哒 【◍´꒳`◍】 |
@xunge0613 别忘了来校对哈 |
=。= 好滴~ 今晚今晚 @sqrthree 前两天加班有点惨 =。=抱歉呀 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
||
Let’s look at a function that leverages the same principle from the arrays example. First we create a function that mutates another value, then we improve the function to be “pure”. | ||
让我们看看一个函数,它利用了数组实例的相同原理。首先我们写一个会改变其它值的函数,然后我们将这个函数优化为“纯”函数。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
让我们看看一个函数,它利用了数组实例的相同原理
=>
我们来看一个示例函数,其原理与前面数组示例的原理相同。
@@ -72,11 +72,11 @@ console.log(add(array, 4)); // [1, 2, 3, 4] | |||
console.log(add(array, 5)); // [1, 2, 3, 4, 5] | |||
``` | |||
|
|||
So again, we are **mutating** our input which creates an unpredictable function. In the functional programming world, there is a golden rule around functions: **a function with the same input should always return the same result**. | |||
所以再一次的,我们**改变**我们的输入来创建一个不可预测的函数。在函数式编程的世界里,有一个关于函数的铁律:**函数对于相同的输入应当返回相同的值。** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
所以再一次的,我们改变我们的输入来创建一个不可预测的函数。
=>
于是我们又一次改变了输入的变量的值,这使得这个函数变得不可预测。
|
||
Let’s see how we can change the implementation of our **add **function so it’s immutable. | ||
让我们来看看怎样修改 **add** 函数的实现来使其不可变。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
让我们来看看怎样修改 add 函数的实现来使其不可变。
=>
让我们来看看怎样修改 add 函数来使其不可变。
精简一下?此处实现感觉可以不译。
> **Note:** You can also use **concat**, instead of **slice** and **push**. | ||
> So: arrayInput.concat(value); | ||
> **注意:**你还可以使用 **concat**,来代替 **slice** 和 **push**。 | ||
> 那样就是:arrayInput.concat(value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
那样就是:
=>
即:
|
||
NodeJS applications use a concept called concurrency. A concurrent operation means that two computations can both make progress regardless of the other. If there are two threads, the second computation doesn’t need to wait for the completion of the first one in order to advance. | ||
NodeJS 的应用有一个叫并发的概念,并发的操作是指两个计算可以同时的进行而不用管另外的一个。如果有两个线程,第二个的计算不需要等待第一个的完成。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
第二个计算不需要等待第一个完成才能继续
=>
第二个计算不需要等待第一个完成才能开始
继续 改成 开始 如何?
|
||
What does immutability have to do with concurrency? Since multiple operations can change a value outside of the function’s scope in a concurrent way, this creates unreliable output and causes unexpected results. Be aware of a function that mutates variables outside of its scope, as this can be really dangerous. | ||
不可变性跟并发又有什么关系呢?由于多个操作可能并发的改变函数的作用域的值,这将会产生不可靠的输出和导致意向不到的结果。明确函数是否改变它作用域之外的值,因为这可能真的会很危险。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
由于多个操作可能并发的改变函数的作用域的值,
=>
由于多个操作可以并发地改变函数作用域之外的值
的 => 地
|
||
Immutability is an important concept to understand on your journey to learn functional programming. You might want to take a look at [ImmutableJS](https://facebook.github.io/immutable-js), written by developers at Facebook. The library provides certain immutable data structures like **Map**, **Set**, and **List**. | ||
不可变性是你学习函数式编程中重要的概念。你也许想了解一下由 Facebook 开发者写的 [ImmutableJS](https://facebook.github.io/immutable-js),这一个库提供正确的不可变数据结构比如说 **Map**、**Set**、和 **List**。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不可变性是你学习函数式编程中重要的概念。
=>
不可变性是学习函数式编程过程中的一个重要概念。
|
||
Immutability is an important concept to understand on your journey to learn functional programming. You might want to take a look at [ImmutableJS](https://facebook.github.io/immutable-js), written by developers at Facebook. The library provides certain immutable data structures like **Map**, **Set**, and **List**. | ||
不可变性是你学习函数式编程中重要的概念。你也许想了解一下由 Facebook 开发者写的 [ImmutableJS](https://facebook.github.io/immutable-js),这一个库提供正确的不可变数据结构比如说 **Map**、**Set**、和 **List**。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
你也许想了解一下由 Facebook 开发者写的
=>
你可以了解一下由 Facebook 开发者写的
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这一个库提供正确的不可变数据结构比如说 Map、Set、和 List。
=>
这个库提供了一些不可变的数据结构,比如:Map、Set、和 List。
certain 此处译为 一些,某些。
@bambooom, @xunge0613 谢谢两位认真校对。 |
1 similar comment
@bambooom, @xunge0613 谢谢两位认真校对。 |
@gy134340 已经 merge 啦~ 快快麻溜发布到掘金专栏然后给我发下链接,方便及时添加积分哟。 |
issues 1684
@sqrthree 翻译初稿