3
In the snippet I prepared below, I have a table with some simple data. Below the table, there are two Abels that account for the total age of the data in the table. The first label is calculated using a common anonymous function, the second is using an Arrow Function.
I realized, at Arrow Function, the this
is Window
, and not the object that called the function, as in the other case. Because of this, the sum on the label results in Nan (Not a Number)
Reading the documentation, I came to the concept of this
But from then on, I didn’t understand anything. How could I get around the situation for Arrow Function to work the way I expected in these cases?
window.onload = function (){
atualizaSomaIdades();
atualizaSomaIdadesArrowFunction();
}
$('.excluir').click(function (){
$(this).parent().parent().remove();
atualizaSomaIdades();
atualizaSomaIdadesArrowFunction();
});
function atualizaSomaIdades(){
var total = 0;
var rows = $('table').find('tbody').find('tr');
rows.each(function () {
total += parseInt($(this).find('td').eq(1).text());
});
$('#idade-comum').text(total);
}
function atualizaSomaIdadesArrowFunction(){
var total = 0;
var rows = $('table').find('tbody').find('tr');
rows.each(() => {
total += parseInt($(this).find('td').eq(1).text());
});
$('#idade-arrow-function').text(total);
}
td{
border: 1px solid black;
}
.negrito{
font-weight: bold;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<thead>
<tr>
<td>Nome</td>
<td>Idade</td>
<td>Ações</td>
</tr>
</thead>
<tbody>
<tr>
<td>
João
</td>
<td>
25
</td>
<td>
<button class="excluir">Remover</button>
</td>
</tr>
<tr>
<td>
Carlos
</td>
<td>
16
</td>
<td>
<button class="excluir">Remover</button>
</td>
</tr>
<tr>
<td>
Artur
</td>
<td>
21
</td>
<td>
<button class="excluir">Remover</button>
</td>
</tr>
</tbody>
</table>
<span class="negrito">Total de idade:</span>
<span id="idade-comum"></span>
<span id="idade-arrow-function"></span>