-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathes.array.for-each.js
54 lines (53 loc) · 1.49 KB
/
es.array.for-each.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { STRICT } from '../helpers/constants.js';
QUnit.test('Array#forEach', assert => {
const { forEach } = Array.prototype;
assert.isFunction(forEach);
assert.arity(forEach, 1);
assert.name(forEach, 'forEach');
assert.looksNative(forEach);
assert.nonEnumerable(Array.prototype, 'forEach');
let array = [1];
const context = {};
array.forEach(function (value, key, that) {
assert.same(arguments.length, 3, 'correct number of callback arguments');
assert.same(value, 1, 'correct value in callback');
assert.same(key, 0, 'correct index in callback');
assert.same(that, array, 'correct link to array in callback');
assert.same(this, context, 'correct callback context');
}, context);
let result = '';
[1, 2, 3].forEach(value => {
result += value;
});
assert.same(result, '123');
result = '';
[1, 2, 3].forEach((value, key) => {
result += key;
});
assert.same(result, '012');
result = '';
[1, 2, 3].forEach((value, key, that) => {
result += that;
});
assert.same(result, '1,2,31,2,31,2,3');
result = '';
[1, 2, 3].forEach(function () {
result += this;
}, 1);
assert.same(result, '111');
result = '';
array = [];
array[5] = '';
array.forEach((value, key) => {
result += key;
});
assert.same(result, '5');
if (STRICT) {
assert.throws(() => {
forEach.call(null, () => { /* empty */ });
}, TypeError);
assert.throws(() => {
forEach.call(undefined, () => { /* empty */ });
}, TypeError);
}
});