Created
May 4, 2019 02:55
-
-
Save johnsi15/13c0dc1a01a51f10152ca67927f9a513 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/zesufa
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
// Con Mutaciones | |
const addToList = (list, item, quantity) => { | |
list.push({ // modificamos el argumento `list` | |
item, | |
quantity | |
}) | |
return list | |
} | |
console.log(addToList([], 'John Serrano', 45)) | |
// Sin mutaciones (inmutabilidad) | |
const addToList_2 = (list, item, quantity) => { | |
const newList = JSON.parse(JSON.stringify(list)) | |
newList.push({ // modificamos la copia del argumento | |
item, | |
quantity | |
}) | |
return newList | |
} | |
console.log(addToList_2([], 'Andrey Serrano', 1200)) | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">// Con Mutaciones | |
const addToList = (list, item, quantity) => { | |
list.push({ // modificamos el argumento `list` | |
item, | |
quantity | |
}) | |
return list | |
} | |
console.log(addToList([], 'John Serrano', 45)) | |
// Sin mutaciones (inmutabilidad) | |
const addToList_2 = (list, item, quantity) => { | |
const newList = JSON.parse(JSON.stringify(list)) | |
newList.push({ // modificamos la copia del argumento | |
item, | |
quantity | |
}) | |
return newList | |
} | |
console.log(addToList_2([], 'Andrey Serrano', 1200))</script></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Con Mutaciones | |
const addToList = (list, item, quantity) => { | |
list.push({ // modificamos el argumento `list` | |
item, | |
quantity | |
}) | |
return list | |
} | |
console.log(addToList([], 'John Serrano', 45)) | |
// Sin mutaciones (inmutabilidad) | |
const addToList_2 = (list, item, quantity) => { | |
const newList = JSON.parse(JSON.stringify(list)) | |
newList.push({ // modificamos la copia del argumento | |
item, | |
quantity | |
}) | |
return newList | |
} | |
console.log(addToList_2([], 'Andrey Serrano', 1200)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment