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]);