You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: JS/JS-br.md
+12-14Lines changed: 12 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -520,13 +520,11 @@ console.log(b.age) // 2
520
520
521
521
A partir do exemplo acima, nós podemos ver que se você assinar um objeto para uma variável, então os valores dos dois vão ter a mesma referência, um muda o outro muda adequadamente.
522
522
523
-
From the above example, we can see that if you assign an object to a variable, then the values of both will be the same reference, one changes, the other changes accordingly.
523
+
Geralmente, nós não queremos que tal problema apareça durante o desensolvimento, portanto podemos usar a cópia rasa para resolver esse problema.
524
524
525
-
Usually, we don't want such problem to appear during development, thus we can use shallow copy to solve this problem.
525
+
## Cópia rasa
526
526
527
-
## Shallow copy
528
-
529
-
Firstly we can solve the problem by `Object.assign`:
527
+
Primeiramente podemos resolver o problema através do `Object.assign`:
530
528
```js
531
529
let a = {
532
530
age:1
@@ -536,7 +534,7 @@ a.age = 2
536
534
console.log(b.age) // 1
537
535
```
538
536
539
-
Certainly, we can use the spread operator (...) to solve the problem:
537
+
Certamente, podemos usar o spread operator (...) para resolver o problema:
540
538
```js
541
539
let a = {
542
540
age:1
@@ -546,7 +544,7 @@ a.age = 2
546
544
console.log(b.age) // 1
547
545
```
548
546
549
-
Usually, shallow copy can solve most problems, but we need deep copy when encountering the following situation:
547
+
Geralmente, a cópia rasa pode resolver a maioria dos problemas, mas precisamos da cópia profunda quando encontrado a seguinte situação:
550
548
```js
551
549
let a = {
552
550
age:1,
@@ -558,11 +556,11 @@ let b = {...a}
558
556
a.jobs.first='native'
559
557
console.log(b.jobs.first) // native
560
558
```
561
-
The shallow copy only solves the problem of the first layer. If the object contains objects, then it returns to the beginning topic that both values share the same reference. To solve this problem, we need to introduce deep copy.
559
+
A cópia rasa resolve apenas o problema na primeira camada. Se o objeto contém objetos, então ele retorna para o topico inicial que os dois valores compartilham a mesma referência. Para resolver esse problema, precisamos introduzir a cópia profunda.
562
560
563
-
## Deep copy
561
+
## Cópia profunda
564
562
565
-
The problem can usually be solved by `JSON.parse(JSON.stringify(object))`
563
+
O problema pode geralmente ser resolvido por`JSON.parse(JSON.stringify(object))`
566
564
567
565
```js
568
566
let a = {
@@ -576,10 +574,10 @@ a.jobs.first = 'native'
576
574
console.log(b.jobs.first) // FE
577
575
```
578
576
579
-
But this method also has its limits:
580
-
* ignore`undefined`
581
-
* unable to serialize function
582
-
* unable to resolve circular references in an object
577
+
Mas esse método também tem seus limites:
578
+
* ignora`undefined`
579
+
* incapaz de serializar função
580
+
* incapaz de resolver referência circular de um objeto
0 commit comments