rest-parameter-aray-iterator.js (1381B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 // Destructuring rest arrays call the array iterator. This behaviour is 6 // observable when Array.prototype[Symbol.iterator] is overridden. 7 8 const oldArrayIterator = Array.prototype[Symbol.iterator]; 9 try { 10 let callCount = 0; 11 Array.prototype[Symbol.iterator] = function() { 12 callCount += 1; 13 return oldArrayIterator.call(this); 14 }; 15 16 // Array iterator called exactly once. 17 function arrayIterCalledOnce(...[]) { } 18 assertEq(callCount, 0); 19 arrayIterCalledOnce(); 20 assertEq(callCount, 1); 21 22 // Array iterator not called before rest parameter. 23 callCount = 0; 24 function arrayIterNotCalledBeforeRest(t = assertEq(callCount, 0), ...[]) { } 25 assertEq(callCount, 0); 26 arrayIterNotCalledBeforeRest(); 27 assertEq(callCount, 1); 28 29 // Array iterator called when rest parameter is processed. 30 callCount = 0; 31 function arrayIterCalledWhenDestructuring(...[t = assertEq(callCount, 1)]) { } 32 assertEq(callCount, 0); 33 arrayIterCalledWhenDestructuring(); 34 assertEq(callCount, 1); 35 } finally { 36 Array.prototype[Symbol.iterator] = oldArrayIterator; 37 } 38 39 if (typeof reportCompare === "function") 40 reportCompare(0, 0);