From 44e21feab5edd5f47b56017fe2bbfe6d9ccb7b7d Mon Sep 17 00:00:00 2001 From: Vitalii Perehonchuk Date: Fri, 5 Aug 2022 13:32:10 +0300 Subject: [PATCH] update(JS): web/javascript/reference/global_objects/array/foreach (#539) * 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 --- .../global_objects/array/foreach/index.md | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/files/uk/web/javascript/reference/global_objects/array/foreach/index.md b/files/uk/web/javascript/reference/global_objects/array/foreach/index.md index 26fa33570d..4151955ce1 100644 --- a/files/uk/web/javascript/reference/global_objects/array/foreach/index.md +++ b/files/uk/web/javascript/reference/global_objects/array/foreach/index.md @@ -139,7 +139,8 @@ forEach(function (element, index, array) { ### Нічого не відбувається на неініціалізованих значеннях (розріджені масиви) ```js -const arraySparse = [1, 3, , 7]; + +const arraySparse = [1, 3, /* пропуск */, 7]; let numCallbackRuns = 0; arraySparse.forEach((element) => { @@ -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]);