caoge c760052ff9 机车状态修改 | 8 mēneši atpakaļ | |
---|---|---|
.. | ||
test | 8 mēneši atpakaļ | |
.editorconfig | 8 mēneši atpakaļ | |
.eslintrc | 8 mēneši atpakaļ | |
.nycrc | 8 mēneši atpakaļ | |
CHANGELOG.md | 8 mēneši atpakaļ | |
LICENSE | 8 mēneši atpakaļ | |
README.md | 8 mēneši atpakaļ | |
auto.js | 8 mēneši atpakaļ | |
implementation.js | 8 mēneši atpakaļ | |
index.js | 8 mēneši atpakaļ | |
package.json | 8 mēneši atpakaļ | |
polyfill.js | 8 mēneši atpakaļ | |
shim.js | 8 mēneši atpakaļ |
An ES5 spec-compliant Array.prototype.reduce
shim/polyfill/replacement that works as far down as ES3.
This package implements the es-shim API interface. It works in an ES3-supported environment and complies with the spec.
Because Array.prototype.reduce
depends on a receiver (the “this” value), the main export takes the array to operate on as the first argument.
var reduce = require('array.prototype.reduce');
var assert = require('assert');
assert.equal(reduce([1, 2, 3], function (prev, x) { return prev + x; }), 6);
assert.equal(reduce([1, 2, 3], function (prev, x) { return prev + x; }, 1), 7);
var reduce = require('array.prototype.reduce');
var assert = require('assert');
/* when Array#reduce is not present */
delete Array.prototype.reduce;
var shimmed = reduce.shim();
assert.equal(shimmed, reduce.getPolyfill());
var arr = [1, 2, 3];
var sum = function (a, b) { return a + b; };
assert.equal(arr.reduce(sum), reduce(arr, sum));
assert.equal(arr.reduce(sum), 6);
assert.equal(arr.reduce(sum, 1), 7);
var reduce = require('array.prototype.reduce');
var assert = require('assert');
/* when Array#reduce is present */
var shimmed = reduce.shim();
assert.equal(shimmed, Array.prototype.reduce);
assert.equal(arr.reduce(sum), reduce(arr, sum));
Simply clone the repo, npm install
, and run npm test