Skip to content

Commit

Permalink
update(JS): web/javascript/reference/global_objects/array/foreach (#539)
Browse files Browse the repository at this point in the history
* update(JS): web/javascript/reference/global_objects/array/foreach/index.md

* update(JS): web/javascript/reference/global_objects/array/foreach

* update(JS): web/javascript/reference/global_objects/array/foreach
  • Loading branch information
undead404 authored Aug 5, 2022
1 parent b33fb56 commit 44e21fe
Showing 1 changed file with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ forEach(function (element, index, array) {
### Нічого не відбувається на неініціалізованих значеннях (розріджені масиви)
```js
const arraySparse = [1, 3, , 7];
<!-- markdownlint-disable-next-line -->
const arraySparse = [1, 3, /* пропуск */, 7];
let numCallbackRuns = 0;
arraySparse.forEach((element) => {
Expand Down Expand Up @@ -203,16 +204,19 @@ const logArrayElements = (element, index, array) => {
Наступний (надуманий) приклад оновлює властивості об'єкта, з урахуванням поданих елементів масиву:

```js
function Counter() {
this.sum = 0;
this.count = 0;
class Counter {
constructor() {
this.sum = 0;
this.count = 0;
}
add(array) {
// Лише вирази функцій матимуть власне зв'язування this
array.forEach(function countEntry(entry) {
this.sum += entry;
++this.count;
}, this);
}
}
Counter.prototype.add = function (array) {
array.forEach(function countEntry(entry) {
this.sum += entry;
++this.count;
}, this);
};

const obj = new Counter();
obj.add([2, 5, 9]);
Expand Down

0 comments on commit 44e21fe

Please sign in to comment.