|
153 | 153 | begin = toInteger(begin);
|
154 | 154 |
|
155 | 155 | // handle -begin, begin > length - 1
|
156 |
| - index = 0 > begin ? Math.max(length + begin, 0) : Math.min(begin, length - 1); |
| 156 | + index = 0 > begin ? length - Math.abs(begin) : Math.min(begin, length - 1); |
157 | 157 | } else {
|
158 | 158 | // default value
|
159 | 159 | index = length - 1;
|
|
456 | 456 | }
|
457 | 457 | }
|
458 | 458 |
|
| 459 | + /** |
| 460 | + * Helper function. |
| 461 | + * Tests implementation of standard Array method. |
| 462 | + */ |
| 463 | + function supportsStandard(key) { |
| 464 | + var support = true; |
| 465 | + if (Array.prototype[key]) { |
| 466 | + try { |
| 467 | + Array.prototype[key].call(undefined, /test/, null); |
| 468 | + support = false; |
| 469 | + } catch (e) {} |
| 470 | + } else { |
| 471 | + support = false; |
| 472 | + } |
| 473 | + return support; |
| 474 | + } |
| 475 | + |
| 476 | + /** |
| 477 | + * Helper function. |
| 478 | + * Tests implementation of generic Array method. |
| 479 | + */ |
| 480 | + function supportsGeneric(key) { |
| 481 | + var support = true; |
| 482 | + if (Array[key]) { |
| 483 | + try { |
| 484 | + Array[key](undefined, /test/, null); |
| 485 | + support = false; |
| 486 | + } catch (e) {} |
| 487 | + } else { |
| 488 | + support = false; |
| 489 | + } |
| 490 | + return support; |
| 491 | + } |
| 492 | + |
459 | 493 | /**
|
460 | 494 | * Helper function.
|
461 | 495 | * Assigns method to Array constructor.
|
462 | 496 | */
|
463 | 497 | function extendArray(key) {
|
464 |
| - Array[key] = Array[key] || makeStatic(key); |
| 498 | + if (!supportsGeneric(key)) { |
| 499 | + Array[key] = makeStatic(key); |
| 500 | + } |
465 | 501 | }
|
466 | 502 |
|
467 | 503 | /**
|
468 | 504 | * Helper function.
|
469 |
| - * Creates static method from an instance method. |
| 505 | + * Creates generic method from an instance method. |
470 | 506 | */
|
471 | 507 | function makeStatic(key) {
|
472 | 508 | return function (value) {
|
|
495 | 531 | "reduce": reduce,
|
496 | 532 | "reduceRight": reduceRight
|
497 | 533 | };
|
| 534 | + |
498 | 535 | for (var key in ES5) {
|
499 | 536 | if (ES5.hasOwnProperty(key)) {
|
| 537 | + |
| 538 | + if (!supportsStandard(key)) { |
| 539 | + Array.prototype[key] = ES5[key]; |
| 540 | + } |
500 | 541 | extendArray(key);
|
501 |
| - Array.prototype[key] = Array.prototype[key] || ES5[key]; |
502 | 542 | }
|
503 | 543 | }
|
504 | 544 | Array.isArray = Array.isArray || isArray;
|
|
0 commit comments