1 | # core-js
|
2 |
|
3 | [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/zloirock/core-js?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [![version](https://img.shields.io/npm/v/core-js.svg)](https://www.npmjs.com/package/core-js) [![npm downloads](https://img.shields.io/npm/dm/core-js.svg)](http://npm-stat.com/charts.html?package=core-js&author=&from=2014-11-18) [![Build Status](https://travis-ci.org/zloirock/core-js.svg)](https://travis-ci.org/zloirock/core-js) [![devDependency status](https://david-dm.org/zloirock/core-js/dev-status.svg)](https://david-dm.org/zloirock/core-js?type=dev)
|
4 | #### As advertising: the author is looking for a good job :)
|
5 |
|
6 | Modular standard library for JavaScript. Includes polyfills for [ECMAScript 5](#ecmascript-5), [ECMAScript 6](#ecmascript-6): [promises](#ecmascript-6-promise), [symbols](#ecmascript-6-symbol), [collections](#ecmascript-6-collections), iterators, [typed arrays](#ecmascript-6-typed-arrays), [ECMAScript 7+ proposals](#ecmascript-7-proposals), [setImmediate](#setimmediate), etc. Some additional features such as [dictionaries](#dict) or [extended partial application](#partial-application). You can require only needed features or use it without global namespace pollution.
|
7 |
|
8 | [*Example*](http://goo.gl/a2xexl):
|
9 | ```js
|
10 | Array.from(new Set([1, 2, 3, 2, 1])); // => [1, 2, 3]
|
11 | '*'.repeat(10); // => '**********'
|
12 | Promise.resolve(32).then(x => console.log(x)); // => 32
|
13 | setImmediate(x => console.log(x), 42); // => 42
|
14 | ```
|
15 |
|
16 | [*Without global namespace pollution*](http://goo.gl/paOHb0):
|
17 | ```js
|
18 | var core = require('core-js/library'); // With a modular system, otherwise use global `core`
|
19 | core.Array.from(new core.Set([1, 2, 3, 2, 1])); // => [1, 2, 3]
|
20 | core.String.repeat('*', 10); // => '**********'
|
21 | core.Promise.resolve(32).then(x => console.log(x)); // => 32
|
22 | core.setImmediate(x => console.log(x), 42); // => 42
|
23 | ```
|
24 |
|
25 | ### Index
|
26 | - [Usage](#usage)
|
27 | - [Basic](#basic)
|
28 | - [CommonJS](#commonjs)
|
29 | - [Custom build](#custom-build-from-the-command-line)
|
30 | - [Supported engines](#supported-engines)
|
31 | - [Features](#features)
|
32 | - [ECMAScript 5](#ecmascript-5)
|
33 | - [ECMAScript 6](#ecmascript-6)
|
34 | - [ECMAScript 6: Object](#ecmascript-6-object)
|
35 | - [ECMAScript 6: Function](#ecmascript-6-function)
|
36 | - [ECMAScript 6: Array](#ecmascript-6-array)
|
37 | - [ECMAScript 6: String](#ecmascript-6-string)
|
38 | - [ECMAScript 6: RegExp](#ecmascript-6-regexp)
|
39 | - [ECMAScript 6: Number](#ecmascript-6-number)
|
40 | - [ECMAScript 6: Math](#ecmascript-6-math)
|
41 | - [ECMAScript 6: Date](#ecmascript-6-date)
|
42 | - [ECMAScript 6: Promise](#ecmascript-6-promise)
|
43 | - [ECMAScript 6: Symbol](#ecmascript-6-symbol)
|
44 | - [ECMAScript 6: Collections](#ecmascript-6-collections)
|
45 | - [ECMAScript 6: Typed Arrays](#ecmascript-6-typed-arrays)
|
46 | - [ECMAScript 6: Reflect](#ecmascript-6-reflect)
|
47 | - [ECMAScript 7+ proposals](#ecmascript-7-proposals)
|
48 | - [stage 4 proposals](#stage-4-proposals)
|
49 | - [stage 3 proposals](#stage-3-proposals)
|
50 | - [stage 2 proposals](#stage-2-proposals)
|
51 | - [stage 1 proposals](#stage-1-proposals)
|
52 | - [stage 0 proposals](#stage-0-proposals)
|
53 | - [pre-stage 0 proposals](#pre-stage-0-proposals)
|
54 | - [Web standards](#web-standards)
|
55 | - [setTimeout / setInterval](#settimeout--setinterval)
|
56 | - [setImmediate](#setimmediate)
|
57 | - [iterable DOM collections](#iterable-dom-collections)
|
58 | - [Non-standard](#non-standard)
|
59 | - [Object](#object)
|
60 | - [Dict](#dict)
|
61 | - [partial application](#partial-application)
|
62 | - [Number Iterator](#number-iterator)
|
63 | - [escaping strings](#escaping-strings)
|
64 | - [delay](#delay)
|
65 | - [helpers for iterators](#helpers-for-iterators)
|
66 | - [Missing polyfills](#missing-polyfills)
|
67 | - [Changelog](./CHANGELOG.md)
|
68 |
|
69 | ## Usage
|
70 | ### Basic
|
71 | ```
|
72 | npm i core-js
|
73 | bower install core.js
|
74 | ```
|
75 |
|
76 | ```js
|
77 | // Default
|
78 | require('core-js');
|
79 | // Without global namespace pollution
|
80 | var core = require('core-js/library');
|
81 | // Shim only
|
82 | require('core-js/shim');
|
83 | ```
|
84 | If you need complete build for browser, use builds from `core-js/client` path:
|
85 |
|
86 | * [default](https://raw.githack.com/zloirock/core-js/v2.6.0/client/core.min.js): Includes all features, standard and non-standard.
|
87 | * [as a library](https://raw.githack.com/zloirock/core-js/v2.6.0/client/library.min.js): Like "default", but does not pollute the global namespace (see [2nd example at the top](#core-js)).
|
88 | * [shim only](https://raw.githack.com/zloirock/core-js/v2.6.0/client/shim.min.js): Only includes the standard methods.
|
89 |
|
90 | Warning: if you use `core-js` with the extension of native objects, require all needed `core-js` modules at the beginning of entry point of your application, otherwise, conflicts may occur.
|
91 |
|
92 | ### CommonJS
|
93 | You can require only needed modules.
|
94 |
|
95 | ```js
|
96 | require('core-js/fn/set');
|
97 | require('core-js/fn/array/from');
|
98 | require('core-js/fn/array/find-index');
|
99 | Array.from(new Set([1, 2, 3, 2, 1])); // => [1, 2, 3]
|
100 | [1, 2, NaN, 3, 4].findIndex(isNaN); // => 2
|
101 |
|
102 | // or, w/o global namespace pollution:
|
103 |
|
104 | var Set = require('core-js/library/fn/set');
|
105 | var from = require('core-js/library/fn/array/from');
|
106 | var findIndex = require('core-js/library/fn/array/find-index');
|
107 | from(new Set([1, 2, 3, 2, 1])); // => [1, 2, 3]
|
108 | findIndex([1, 2, NaN, 3, 4], isNaN); // => 2
|
109 | ```
|
110 | Available entry points for methods / constructors, as above examples, and namespaces: for example, `core-js/es6/array` (`core-js/library/es6/array`) contains all [ES6 `Array` features](#ecmascript-6-array), `core-js/es6` (`core-js/library/es6`) contains all ES6 features.
|
111 |
|
112 | ##### Caveats when using CommonJS API:
|
113 |
|
114 | * `modules` path is internal API, does not inject all required dependencies and can be changed in minor or patch releases. Use it only for a custom build and / or if you know what are you doing.
|
115 | * `core-js` is extremely modular and uses a lot of very tiny modules, because of that for usage in browsers bundle up `core-js` instead of usage loader for each file, otherwise, you will have hundreds of requests.
|
116 |
|
117 | #### CommonJS and prototype methods without global namespace pollution
|
118 | In the `library` version, we can't pollute prototypes of native constructors. Because of that, prototype methods transformed to static methods like in examples above. `babel` `runtime` transformer also can't transform them. But with transpilers we can use one more trick - [bind operator and virtual methods](https://github.com/zenparsing/es-function-bind). Special for that, available `/virtual/` entry points. Example:
|
119 | ```js
|
120 | import fill from 'core-js/library/fn/array/virtual/fill';
|
121 | import findIndex from 'core-js/library/fn/array/virtual/find-index';
|
122 |
|
123 | Array(10)::fill(0).map((a, b) => b * b)::findIndex(it => it && !(it % 8)); // => 4
|
124 |
|
125 | // or
|
126 |
|
127 | import {fill, findIndex} from 'core-js/library/fn/array/virtual';
|
128 |
|
129 | Array(10)::fill(0).map((a, b) => b * b)::findIndex(it => it && !(it % 8)); // => 4
|
130 |
|
131 | ```
|
132 |
|
133 | ### Custom build (from the command-line)
|
134 | ```
|
135 | npm i core-js && cd node_modules/core-js && npm i
|
136 | npm run grunt build:core.dict,es6 -- --blacklist=es6.promise,es6.math --library=on --path=custom uglify
|
137 | ```
|
138 | Where `core.dict` and `es6` are modules (namespaces) names, which will be added to the build, `es6.promise` and `es6.math` are modules (namespaces) names, which will be excluded from the build, `--library=on` is flag for build without global namespace pollution and `custom` is target file name.
|
139 |
|
140 | Available namespaces: for example, `es6.array` contains [ES6 `Array` features](#ecmascript-6-array), `es6` contains all modules whose names start with `es6`.
|
141 |
|
142 | ### Custom build (from external scripts)
|
143 |
|
144 | [`core-js-builder`](https://www.npmjs.com/package/core-js-builder) package exports a function that takes the same parameters as the `build` target from the previous section. This will conditionally include or exclude certain parts of `core-js`:
|
145 |
|
146 | ```js
|
147 | require('core-js-builder')({
|
148 | modules: ['es6', 'core.dict'], // modules / namespaces
|
149 | blacklist: ['es6.reflect'], // blacklist of modules / namespaces, by default - empty list
|
150 | library: false, // flag for build without global namespace pollution, by default - false
|
151 | umd: true // use UMD wrapper for export `core` object, by default - true
|
152 | }).then(code => {
|
153 | // ...
|
154 | }).catch(error => {
|
155 | // ...
|
156 | });
|
157 | ```
|
158 | ## Supported engines
|
159 | **Tested in:**
|
160 | - Chrome 26+
|
161 | - Firefox 4+
|
162 | - Safari 5+
|
163 | - Opera 12+
|
164 | - Internet Explorer 6+ (sure, IE8- with ES3 limitations)
|
165 | - Edge
|
166 | - Android Browser 2.3+
|
167 | - iOS Safari 5.1+
|
168 | - PhantomJS 1.9 / 2.1
|
169 | - NodeJS 0.8+
|
170 |
|
171 | ...and it doesn't mean `core-js` will not work in other engines, they just have not been tested.
|
172 |
|
173 | ## Features:
|
174 | [*CommonJS entry points:*](#commonjs)
|
175 | ```
|
176 | core-js(/library) <- all features
|
177 | core-js(/library)/shim <- only polyfills
|
178 | ```
|
179 | ### ECMAScript 5
|
180 | All features moved to the [`es6` namespace](#ecmascript-6), here just a list of features:
|
181 | ```js
|
182 | Object
|
183 | .create(proto | null, descriptors?) -> object
|
184 | .getPrototypeOf(object) -> proto | null
|
185 | .defineProperty(target, key, desc) -> target, cap for ie8-
|
186 | .defineProperties(target, descriptors) -> target, cap for ie8-
|
187 | .getOwnPropertyDescriptor(object, key) -> desc
|
188 | .getOwnPropertyNames(object) -> array
|
189 | .keys(object) -> array
|
190 | .seal(object) -> object, cap for ie8-
|
191 | .freeze(object) -> object, cap for ie8-
|
192 | .preventExtensions(object) -> object, cap for ie8-
|
193 | .isSealed(object) -> bool, cap for ie8-
|
194 | .isFrozen(object) -> bool, cap for ie8-
|
195 | .isExtensible(object) -> bool, cap for ie8-
|
196 | Array
|
197 | .isArray(var) -> bool
|
198 | #slice(start?, end?) -> array, fix for ie7-
|
199 | #join(string = ',') -> string, fix for ie7-
|
200 | #indexOf(var, from?) -> int
|
201 | #lastIndexOf(var, from?) -> int
|
202 | #every(fn(val, index, @), that) -> bool
|
203 | #some(fn(val, index, @), that) -> bool
|
204 | #forEach(fn(val, index, @), that) -> void
|
205 | #map(fn(val, index, @), that) -> array
|
206 | #filter(fn(val, index, @), that) -> array
|
207 | #reduce(fn(memo, val, index, @), memo?) -> var
|
208 | #reduceRight(fn(memo, val, index, @), memo?) -> var
|
209 | #sort(fn?) -> @, fixes for some engines
|
210 | Function
|
211 | #bind(object, ...args) -> boundFn(...args)
|
212 | String
|
213 | #split(separator, limit) -> array
|
214 | #trim() -> str
|
215 | RegExp
|
216 | #toString() -> str
|
217 | Number
|
218 | #toFixed(digits) -> string
|
219 | #toPrecision(precision) -> string
|
220 | parseInt(str, radix) -> int
|
221 | parseFloat(str) -> num
|
222 | Date
|
223 | .now() -> int
|
224 | #toISOString() -> string
|
225 | #toJSON() -> string
|
226 | ```
|
227 | [*CommonJS entry points:*](#commonjs)
|
228 | ```
|
229 | core-js(/library)/es5
|
230 | ```
|
231 |
|
232 | ### ECMAScript 6
|
233 | [*CommonJS entry points:*](#commonjs)
|
234 | ```
|
235 | core-js(/library)/es6
|
236 | ```
|
237 | #### ECMAScript 6: Object
|
238 | Modules [`es6.object.assign`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.object.assign.js), [`es6.object.is`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.object.is.js), [`es6.object.set-prototype-of`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.object.set-prototype-of.js) and [`es6.object.to-string`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.object.to-string.js).
|
239 |
|
240 | In ES6 most `Object` static methods should work with primitives. Modules [`es6.object.freeze`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.object.freeze.js), [`es6.object.seal`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.object.seal.js), [`es6.object.prevent-extensions`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.object.prevent-extensions.js), [`es6.object.is-frozen`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.object.is-frozen.js), [`es6.object.is-sealed`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.object.is-sealed.js), [`es6.object.is-extensible`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.object.is-extensible.js), [`es6.object.get-own-property-descriptor`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.object.get-own-property-descriptor.js), [`es6.object.get-prototype-of`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.object.get-prototype-of.js), [`es6.object.keys`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.object.keys.js) and [`es6.object.get-own-property-names`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.object.get-own-property-names.js).
|
241 |
|
242 | Just ES5 features: [`es6.object.create`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.object.create.js), [`es6.object.define-property`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.object.define-property.js) and [`es6.object.define-properties`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.object.es6.object.define-properties.js).
|
243 | ```js
|
244 | Object
|
245 | .assign(target, ...src) -> target
|
246 | .is(a, b) -> bool
|
247 | .setPrototypeOf(target, proto | null) -> target (required __proto__ - IE11+)
|
248 | .create(object | null, descriptors?) -> object
|
249 | .getPrototypeOf(var) -> object | null
|
250 | .defineProperty(object, key, desc) -> target
|
251 | .defineProperties(object, descriptors) -> target
|
252 | .getOwnPropertyDescriptor(var, key) -> desc | undefined
|
253 | .keys(var) -> array
|
254 | .getOwnPropertyNames(var) -> array
|
255 | .freeze(var) -> var
|
256 | .seal(var) -> var
|
257 | .preventExtensions(var) -> var
|
258 | .isFrozen(var) -> bool
|
259 | .isSealed(var) -> bool
|
260 | .isExtensible(var) -> bool
|
261 | #toString() -> string, ES6 fix: @@toStringTag support
|
262 | ```
|
263 | [*CommonJS entry points:*](#commonjs)
|
264 | ```
|
265 | core-js(/library)/es6/object
|
266 | core-js(/library)/fn/object/assign
|
267 | core-js(/library)/fn/object/is
|
268 | core-js(/library)/fn/object/set-prototype-of
|
269 | core-js(/library)/fn/object/get-prototype-of
|
270 | core-js(/library)/fn/object/create
|
271 | core-js(/library)/fn/object/define-property
|
272 | core-js(/library)/fn/object/define-properties
|
273 | core-js(/library)/fn/object/get-own-property-descriptor
|
274 | core-js(/library)/fn/object/keys
|
275 | core-js(/library)/fn/object/get-own-property-names
|
276 | core-js(/library)/fn/object/freeze
|
277 | core-js(/library)/fn/object/seal
|
278 | core-js(/library)/fn/object/prevent-extensions
|
279 | core-js(/library)/fn/object/is-frozen
|
280 | core-js(/library)/fn/object/is-sealed
|
281 | core-js(/library)/fn/object/is-extensible
|
282 | core-js/fn/object/to-string
|
283 | ```
|
284 | [*Examples*](http://goo.gl/ywdwPz):
|
285 | ```js
|
286 | var foo = {q: 1, w: 2}
|
287 | , bar = {e: 3, r: 4}
|
288 | , baz = {t: 5, y: 6};
|
289 | Object.assign(foo, bar, baz); // => foo = {q: 1, w: 2, e: 3, r: 4, t: 5, y: 6}
|
290 |
|
291 | Object.is(NaN, NaN); // => true
|
292 | Object.is(0, -0); // => false
|
293 | Object.is(42, 42); // => true
|
294 | Object.is(42, '42'); // => false
|
295 |
|
296 | function Parent(){}
|
297 | function Child(){}
|
298 | Object.setPrototypeOf(Child.prototype, Parent.prototype);
|
299 | new Child instanceof Child; // => true
|
300 | new Child instanceof Parent; // => true
|
301 |
|
302 | var O = {};
|
303 | O[Symbol.toStringTag] = 'Foo';
|
304 | '' + O; // => '[object Foo]'
|
305 |
|
306 | Object.keys('qwe'); // => ['0', '1', '2']
|
307 | Object.getPrototypeOf('qwe') === String.prototype; // => true
|
308 | ```
|
309 | #### ECMAScript 6: Function
|
310 | Modules [`es6.function.name`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.function.name.js), [`es6.function.has-instance`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.function.has-instance.js). Just ES5: [`es6.function.bind`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.function.bind.js).
|
311 | ```js
|
312 | Function
|
313 | #bind(object, ...args) -> boundFn(...args)
|
314 | #name -> string (IE9+)
|
315 | #@@hasInstance(var) -> bool
|
316 | ```
|
317 | [*CommonJS entry points:*](#commonjs)
|
318 | ```
|
319 | core-js/es6/function
|
320 | core-js/fn/function/name
|
321 | core-js/fn/function/has-instance
|
322 | core-js/fn/function/bind
|
323 | core-js/fn/function/virtual/bind
|
324 | ```
|
325 | [*Example*](http://goo.gl/zqu3Wp):
|
326 | ```js
|
327 | (function foo(){}).name // => 'foo'
|
328 |
|
329 | console.log.bind(console, 42)(43); // => 42 43
|
330 | ```
|
331 | #### ECMAScript 6: Array
|
332 | Modules [`es6.array.from`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.array.from.js), [`es6.array.of`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.array.of.js), [`es6.array.copy-within`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.array.copy-within.js), [`es6.array.fill`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.array.fill.js), [`es6.array.find`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.array.find.js), [`es6.array.find-index`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.array.find-index.js), [`es6.array.iterator`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.array.iterator.js). ES5 features with fixes: [`es6.array.is-array`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.array.is-array.js), [`es6.array.slice`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.array.slice.js), [`es6.array.join`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.array.join.js), [`es6.array.index-of`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.array.index-of.js), [`es6.array.last-index-of`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.array.last-index-of.js), [`es6.array.every`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.array.every.js), [`es6.array.some`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.array.some.js), [`es6.array.for-each`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.array.for-each.js), [`es6.array.map`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.array.map.js), [`es6.array.filter`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.array.filter.js), [`es6.array.reduce`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.array.reduce.js), [`es6.array.reduce-right`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.array.reduce-right.js), [`es6.array.sort`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.array.sort.js).
|
333 | ```js
|
334 | Array
|
335 | .from(iterable | array-like, mapFn(val, index)?, that) -> array
|
336 | .of(...args) -> array
|
337 | .isArray(var) -> bool
|
338 | #copyWithin(target = 0, start = 0, end = @length) -> @
|
339 | #fill(val, start = 0, end = @length) -> @
|
340 | #find(fn(val, index, @), that) -> val
|
341 | #findIndex(fn(val, index, @), that) -> index | -1
|
342 | #values() -> iterator
|
343 | #keys() -> iterator
|
344 | #entries() -> iterator
|
345 | #join(string = ',') -> string, fix for ie7-
|
346 | #slice(start?, end?) -> array, fix for ie7-
|
347 | #indexOf(var, from?) -> index | -1
|
348 | #lastIndexOf(var, from?) -> index | -1
|
349 | #every(fn(val, index, @), that) -> bool
|
350 | #some(fn(val, index, @), that) -> bool
|
351 | #forEach(fn(val, index, @), that) -> void
|
352 | #map(fn(val, index, @), that) -> array
|
353 | #filter(fn(val, index, @), that) -> array
|
354 | #reduce(fn(memo, val, index, @), memo?) -> var
|
355 | #reduceRight(fn(memo, val, index, @), memo?) -> var
|
356 | #sort(fn?) -> @, invalid arguments fix
|
357 | #@@iterator() -> iterator (values)
|
358 | #@@unscopables -> object (cap)
|
359 | Arguments
|
360 | #@@iterator() -> iterator (values, available only in core-js methods)
|
361 | ```
|
362 | [*CommonJS entry points:*](#commonjs)
|
363 | ```
|
364 | core-js(/library)/es6/array
|
365 | core-js(/library)/fn/array/from
|
366 | core-js(/library)/fn/array/of
|
367 | core-js(/library)/fn/array/is-array
|
368 | core-js(/library)/fn/array/iterator
|
369 | core-js(/library)/fn/array/copy-within
|
370 | core-js(/library)/fn/array/fill
|
371 | core-js(/library)/fn/array/find
|
372 | core-js(/library)/fn/array/find-index
|
373 | core-js(/library)/fn/array/values
|
374 | core-js(/library)/fn/array/keys
|
375 | core-js(/library)/fn/array/entries
|
376 | core-js(/library)/fn/array/slice
|
377 | core-js(/library)/fn/array/join
|
378 | core-js(/library)/fn/array/index-of
|
379 | core-js(/library)/fn/array/last-index-of
|
380 | core-js(/library)/fn/array/every
|
381 | core-js(/library)/fn/array/some
|
382 | core-js(/library)/fn/array/for-each
|
383 | core-js(/library)/fn/array/map
|
384 | core-js(/library)/fn/array/filter
|
385 | core-js(/library)/fn/array/reduce
|
386 | core-js(/library)/fn/array/reduce-right
|
387 | core-js(/library)/fn/array/sort
|
388 | core-js(/library)/fn/array/virtual/iterator
|
389 | core-js(/library)/fn/array/virtual/copy-within
|
390 | core-js(/library)/fn/array/virtual/fill
|
391 | core-js(/library)/fn/array/virtual/find
|
392 | core-js(/library)/fn/array/virtual/find-index
|
393 | core-js(/library)/fn/array/virtual/values
|
394 | core-js(/library)/fn/array/virtual/keys
|
395 | core-js(/library)/fn/array/virtual/entries
|
396 | core-js(/library)/fn/array/virtual/slice
|
397 | core-js(/library)/fn/array/virtual/join
|
398 | core-js(/library)/fn/array/virtual/index-of
|
399 | core-js(/library)/fn/array/virtual/last-index-of
|
400 | core-js(/library)/fn/array/virtual/every
|
401 | core-js(/library)/fn/array/virtual/some
|
402 | core-js(/library)/fn/array/virtual/for-each
|
403 | core-js(/library)/fn/array/virtual/map
|
404 | core-js(/library)/fn/array/virtual/filter
|
405 | core-js(/library)/fn/array/virtual/reduce
|
406 | core-js(/library)/fn/array/virtual/reduce-right
|
407 | core-js(/library)/fn/array/virtual/sort
|
408 | ```
|
409 | [*Examples*](http://goo.gl/oaUFUf):
|
410 | ```js
|
411 | Array.from(new Set([1, 2, 3, 2, 1])); // => [1, 2, 3]
|
412 | Array.from({0: 1, 1: 2, 2: 3, length: 3}); // => [1, 2, 3]
|
413 | Array.from('123', Number); // => [1, 2, 3]
|
414 | Array.from('123', function(it){
|
415 | return it * it;
|
416 | }); // => [1, 4, 9]
|
417 |
|
418 | Array.of(1); // => [1]
|
419 | Array.of(1, 2, 3); // => [1, 2, 3]
|
420 |
|
421 | var array = ['a', 'b', 'c'];
|
422 |
|
423 | for(var val of array)console.log(val); // => 'a', 'b', 'c'
|
424 | for(var val of array.values())console.log(val); // => 'a', 'b', 'c'
|
425 | for(var key of array.keys())console.log(key); // => 0, 1, 2
|
426 | for(var [key, val] of array.entries()){
|
427 | console.log(key); // => 0, 1, 2
|
428 | console.log(val); // => 'a', 'b', 'c'
|
429 | }
|
430 |
|
431 | function isOdd(val){
|
432 | return val % 2;
|
433 | }
|
434 | [4, 8, 15, 16, 23, 42].find(isOdd); // => 15
|
435 | [4, 8, 15, 16, 23, 42].findIndex(isOdd); // => 2
|
436 | [4, 8, 15, 16, 23, 42].find(isNaN); // => undefined
|
437 | [4, 8, 15, 16, 23, 42].findIndex(isNaN); // => -1
|
438 |
|
439 | Array(5).fill(42); // => [42, 42, 42, 42, 42]
|
440 |
|
441 | [1, 2, 3, 4, 5].copyWithin(0, 3); // => [4, 5, 3, 4, 5]
|
442 | ```
|
443 | #### ECMAScript 6: String
|
444 | Modules [`es6.string.from-code-point`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.string.from-code-point.js), [`es6.string.raw`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.string.raw.js), [`es6.string.iterator`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.string.iterator.js), [`es6.string.code-point-at`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.string.code-point-at.js), [`es6.string.ends-with`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.string.ends-with.js), [`es6.string.includes`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.string.includes.js), [`es6.string.repeat`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.string.repeat.js), [`es6.string.starts-with`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.string.starts-with.js) and [`es6.string.trim`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.string.trim.js).
|
445 |
|
446 | Annex B HTML methods. Ugly, but it's also the part of the spec. Modules [`es6.string.anchor`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.string.anchor.js), [`es6.string.big`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.string.big.js), [`es6.string.blink`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.string.blink.js), [`es6.string.bold`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.string.bold.js), [`es6.string.fixed`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.string.fixed.js), [`es6.string.fontcolor`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.string.fontcolor.js), [`es6.string.fontsize`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.string.fontsize.js), [`es6.string.italics`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.string.italics.js), [`es6.string.link`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.string.link.js), [`es6.string.small`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.string.small.js), [`es6.string.strike`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.string.strike.js), [`es6.string.sub`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.string.sub.js) and [`es6.string.sup`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.string.sup.js).
|
447 | ```js
|
448 | String
|
449 | .fromCodePoint(...codePoints) -> str
|
450 | .raw({raw}, ...substitutions) -> str
|
451 | #includes(str, from?) -> bool
|
452 | #startsWith(str, from?) -> bool
|
453 | #endsWith(str, from?) -> bool
|
454 | #repeat(num) -> str
|
455 | #codePointAt(pos) -> uint
|
456 | #trim() -> str, ES6 fix
|
457 | #anchor(name) -> str
|
458 | #big() -> str
|
459 | #blink() -> str
|
460 | #bold() -> str
|
461 | #fixed() -> str
|
462 | #fontcolor(color) -> str
|
463 | #fontsize(size) -> str
|
464 | #italics() -> str
|
465 | #link(url) -> str
|
466 | #small() -> str
|
467 | #strike() -> str
|
468 | #sub() -> str
|
469 | #sup() -> str
|
470 | #@@iterator() -> iterator (code points)
|
471 | ```
|
472 | [*CommonJS entry points:*](#commonjs)
|
473 | ```
|
474 | core-js(/library)/es6/string
|
475 | core-js(/library)/fn/string/from-code-point
|
476 | core-js(/library)/fn/string/raw
|
477 | core-js(/library)/fn/string/includes
|
478 | core-js(/library)/fn/string/starts-with
|
479 | core-js(/library)/fn/string/ends-with
|
480 | core-js(/library)/fn/string/repeat
|
481 | core-js(/library)/fn/string/code-point-at
|
482 | core-js(/library)/fn/string/trim
|
483 | core-js(/library)/fn/string/anchor
|
484 | core-js(/library)/fn/string/big
|
485 | core-js(/library)/fn/string/blink
|
486 | core-js(/library)/fn/string/bold
|
487 | core-js(/library)/fn/string/fixed
|
488 | core-js(/library)/fn/string/fontcolor
|
489 | core-js(/library)/fn/string/fontsize
|
490 | core-js(/library)/fn/string/italics
|
491 | core-js(/library)/fn/string/link
|
492 | core-js(/library)/fn/string/small
|
493 | core-js(/library)/fn/string/strike
|
494 | core-js(/library)/fn/string/sub
|
495 | core-js(/library)/fn/string/sup
|
496 | core-js(/library)/fn/string/iterator
|
497 | core-js(/library)/fn/string/virtual/includes
|
498 | core-js(/library)/fn/string/virtual/starts-with
|
499 | core-js(/library)/fn/string/virtual/ends-with
|
500 | core-js(/library)/fn/string/virtual/repeat
|
501 | core-js(/library)/fn/string/virtual/code-point-at
|
502 | core-js(/library)/fn/string/virtual/trim
|
503 | core-js(/library)/fn/string/virtual/anchor
|
504 | core-js(/library)/fn/string/virtual/big
|
505 | core-js(/library)/fn/string/virtual/blink
|
506 | core-js(/library)/fn/string/virtual/bold
|
507 | core-js(/library)/fn/string/virtual/fixed
|
508 | core-js(/library)/fn/string/virtual/fontcolor
|
509 | core-js(/library)/fn/string/virtual/fontsize
|
510 | core-js(/library)/fn/string/virtual/italics
|
511 | core-js(/library)/fn/string/virtual/link
|
512 | core-js(/library)/fn/string/virtual/small
|
513 | core-js(/library)/fn/string/virtual/strike
|
514 | core-js(/library)/fn/string/virtual/sub
|
515 | core-js(/library)/fn/string/virtual/sup
|
516 | core-js(/library)/fn/string/virtual/iterator
|
517 | ```
|
518 | [*Examples*](http://goo.gl/3UaQ93):
|
519 | ```js
|
520 | for(var val of 'a𠮷b'){
|
521 | console.log(val); // => 'a', '𠮷', 'b'
|
522 | }
|
523 |
|
524 | 'foobarbaz'.includes('bar'); // => true
|
525 | 'foobarbaz'.includes('bar', 4); // => false
|
526 | 'foobarbaz'.startsWith('foo'); // => true
|
527 | 'foobarbaz'.startsWith('bar', 3); // => true
|
528 | 'foobarbaz'.endsWith('baz'); // => true
|
529 | 'foobarbaz'.endsWith('bar', 6); // => true
|
530 |
|
531 | 'string'.repeat(3); // => 'stringstringstring'
|
532 |
|
533 | '𠮷'.codePointAt(0); // => 134071
|
534 | String.fromCodePoint(97, 134071, 98); // => 'a𠮷b'
|
535 |
|
536 | var name = 'Bob';
|
537 | String.raw`Hi\n${name}!`; // => 'Hi\\nBob!' (ES6 template string syntax)
|
538 | String.raw({raw: 'test'}, 0, 1, 2); // => 't0e1s2t'
|
539 |
|
540 | 'foo'.bold(); // => '<b>foo</b>'
|
541 | 'bar'.anchor('a"b'); // => '<a name="a"b">bar</a>'
|
542 | 'baz'.link('http://example.com'); // => '<a href="http://example.com">baz</a>'
|
543 | ```
|
544 | #### ECMAScript 6: RegExp
|
545 | Modules [`es6.regexp.constructor`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.regexp.constructor.js) and [`es6.regexp.flags`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.regexp.flags.js).
|
546 |
|
547 | Support well-known [symbols](#ecmascript-6-symbol) `@@match`, `@@replace`, `@@search` and `@@split`, modules [`es6.regexp.match`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.regexp.match.js), [`es6.regexp.replace`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.regexp.replace.js), [`es6.regexp.search`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.regexp.search.js) and [`es6.regexp.split`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.regexp.split.js).
|
548 | ```
|
549 | [new] RegExp(pattern, flags?) -> regexp, ES6 fix: can alter flags (IE9+)
|
550 | #flags -> str (IE9+)
|
551 | #toString() -> str, ES6 fixes
|
552 | #@@match(str) -> array | null
|
553 | #@@replace(str, replacer) -> string
|
554 | #@@search(str) -> index
|
555 | #@@split(str, limit) -> array
|
556 | String
|
557 | #match(tpl) -> var, ES6 fix for support @@match
|
558 | #replace(tpl, replacer) -> var, ES6 fix for support @@replace
|
559 | #search(tpl) -> var, ES6 fix for support @@search
|
560 | #split(tpl, limit) -> var, ES6 fix for support @@split, some fixes for old engines
|
561 | ```
|
562 | [*CommonJS entry points:*](#commonjs)
|
563 | ```
|
564 | core-js/es6/regexp
|
565 | core-js/fn/regexp/constructor
|
566 | core-js(/library)/fn/regexp/flags
|
567 | core-js/fn/regexp/to-string
|
568 | core-js/fn/regexp/match
|
569 | core-js/fn/regexp/replace
|
570 | core-js/fn/regexp/search
|
571 | core-js/fn/regexp/split
|
572 | ```
|
573 | [*Examples*](http://goo.gl/PiJxBD):
|
574 | ```js
|
575 | RegExp(/./g, 'm'); // => /./m
|
576 |
|
577 | /foo/.flags; // => ''
|
578 | /foo/gim.flags; // => 'gim'
|
579 |
|
580 | 'foo'.match({[Symbol.match]: _ => 1}); // => 1
|
581 | 'foo'.replace({[Symbol.replace]: _ => 2}); // => 2
|
582 | 'foo'.search({[Symbol.search]: _ => 3}); // => 3
|
583 | 'foo'.split({[Symbol.split]: _ => 4}); // => 4
|
584 |
|
585 | RegExp.prototype.toString.call({source: 'foo', flags: 'bar'}); // => '/foo/bar'
|
586 | ```
|
587 | #### ECMAScript 6: Number
|
588 | Module [`es6.number.constructor`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.number.constructor.js). `Number` constructor support binary and octal literals, [*example*](http://goo.gl/jRd6b3):
|
589 | ```js
|
590 | Number('0b1010101'); // => 85
|
591 | Number('0o7654321'); // => 2054353
|
592 | ```
|
593 | Modules [`es6.number.epsilon`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.number.epsilon.js), [`es6.number.is-finite`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.number.is-finite.js), [`es6.number.is-integer`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.number.is-integer.js), [`es6.number.is-nan`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.number.is-nan.js), [`es6.number.is-safe-integer`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.number.is-safe-integer.js), [`es6.number.max-safe-integer`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.number.max-safe-integer.js), [`es6.number.min-safe-integer`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.number.min-safe-integer.js), [`es6.number.parse-float`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.number.parse-float.js), [`es6.number.parse-int`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.number.parse-int.js), [`es6.number.to-fixed`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.number.to-fixed.js), [`es6.number.to-precision`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.number.to-precision.js), [`es6.parse-int`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.parse-int.js), [`es6.parse-float`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.parse-float.js).
|
594 | ```js
|
595 | [new] Number(var) -> number | number object
|
596 | .isFinite(num) -> bool
|
597 | .isNaN(num) -> bool
|
598 | .isInteger(num) -> bool
|
599 | .isSafeInteger(num) -> bool
|
600 | .parseFloat(str) -> num
|
601 | .parseInt(str) -> int
|
602 | .EPSILON -> num
|
603 | .MAX_SAFE_INTEGER -> int
|
604 | .MIN_SAFE_INTEGER -> int
|
605 | #toFixed(digits) -> string, fixes
|
606 | #toPrecision(precision) -> string, fixes
|
607 | parseFloat(str) -> num, fixes
|
608 | parseInt(str) -> int, fixes
|
609 | ```
|
610 | [*CommonJS entry points:*](#commonjs)
|
611 | ```
|
612 | core-js(/library)/es6/number
|
613 | core-js/es6/number/constructor
|
614 | core-js(/library)/fn/number/is-finite
|
615 | core-js(/library)/fn/number/is-nan
|
616 | core-js(/library)/fn/number/is-integer
|
617 | core-js(/library)/fn/number/is-safe-integer
|
618 | core-js(/library)/fn/number/parse-float
|
619 | core-js(/library)/fn/number/parse-int
|
620 | core-js(/library)/fn/number/epsilon
|
621 | core-js(/library)/fn/number/max-safe-integer
|
622 | core-js(/library)/fn/number/min-safe-integer
|
623 | core-js(/library)/fn/number/to-fixed
|
624 | core-js(/library)/fn/number/to-precision
|
625 | core-js(/library)/fn/parse-float
|
626 | core-js(/library)/fn/parse-int
|
627 | ```
|
628 | #### ECMAScript 6: Math
|
629 | Modules [`es6.math.acosh`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.math.acosh.js), [`es6.math.asinh`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.math.asinh.js), [`es6.math.atanh`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.math.atanh.js), [`es6.math.cbrt`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.math.cbrt.js), [`es6.math.clz32`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.math.clz32.js), [`es6.math.cosh`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.math.cosh.js), [`es6.math.expm1`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.math.expm1.js), [`es6.math.fround`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.math.fround.js), [`es6.math.hypot`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.math.hypot.js), [`es6.math.imul`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.math.imul.js), [`es6.math.log10`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.math.log10.js), [`es6.math.log1p`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.math.log1p.js), [`es6.math.log2`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.math.log2.js), [`es6.math.sign`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.math.sign.js), [`es6.math.sinh`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.math.sinh.js), [`es6.math.tanh`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.math.tanh.js), [`es6.math.trunc`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.math.trunc.js).
|
630 | ```js
|
631 | Math
|
632 | .acosh(num) -> num
|
633 | .asinh(num) -> num
|
634 | .atanh(num) -> num
|
635 | .cbrt(num) -> num
|
636 | .clz32(num) -> uint
|
637 | .cosh(num) -> num
|
638 | .expm1(num) -> num
|
639 | .fround(num) -> num
|
640 | .hypot(...args) -> num
|
641 | .imul(num, num) -> int
|
642 | .log1p(num) -> num
|
643 | .log10(num) -> num
|
644 | .log2(num) -> num
|
645 | .sign(num) -> 1 | -1 | 0 | -0 | NaN
|
646 | .sinh(num) -> num
|
647 | .tanh(num) -> num
|
648 | .trunc(num) -> num
|
649 | ```
|
650 | [*CommonJS entry points:*](#commonjs)
|
651 | ```
|
652 | core-js(/library)/es6/math
|
653 | core-js(/library)/fn/math/acosh
|
654 | core-js(/library)/fn/math/asinh
|
655 | core-js(/library)/fn/math/atanh
|
656 | core-js(/library)/fn/math/cbrt
|
657 | core-js(/library)/fn/math/clz32
|
658 | core-js(/library)/fn/math/cosh
|
659 | core-js(/library)/fn/math/expm1
|
660 | core-js(/library)/fn/math/fround
|
661 | core-js(/library)/fn/math/hypot
|
662 | core-js(/library)/fn/math/imul
|
663 | core-js(/library)/fn/math/log1p
|
664 | core-js(/library)/fn/math/log10
|
665 | core-js(/library)/fn/math/log2
|
666 | core-js(/library)/fn/math/sign
|
667 | core-js(/library)/fn/math/sinh
|
668 | core-js(/library)/fn/math/tanh
|
669 | core-js(/library)/fn/math/trunc
|
670 | ```
|
671 | #### ECMAScript 6: Date
|
672 | Modules [`es6.date.to-string`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.date.to-string.js), ES5 features with fixes: [`es6.date.now`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.date.now.js), [`es6.date.to-iso-string`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.date.to-iso-string.js), [`es6.date.to-json`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.date.to-json.js) and [`es6.date.to-primitive`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.date.to-primitive.js).
|
673 | ```js
|
674 | Date
|
675 | .now() -> int
|
676 | #toISOString() -> string
|
677 | #toJSON() -> string
|
678 | #toString() -> string
|
679 | #@@toPrimitive(hint) -> primitive
|
680 | ```
|
681 | [*CommonJS entry points:*](#commonjs)
|
682 | ```
|
683 | core-js/es6/date
|
684 | core-js/fn/date/to-string
|
685 | core-js(/library)/fn/date/now
|
686 | core-js(/library)/fn/date/to-iso-string
|
687 | core-js(/library)/fn/date/to-json
|
688 | core-js(/library)/fn/date/to-primitive
|
689 | ```
|
690 | [*Example*](http://goo.gl/haeHLR):
|
691 | ```js
|
692 | new Date(NaN).toString(); // => 'Invalid Date'
|
693 | ```
|
694 |
|
695 | #### ECMAScript 6: Promise
|
696 | Module [`es6.promise`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.promise.js).
|
697 | ```js
|
698 | new Promise(executor(resolve(var), reject(var))) -> promise
|
699 | #then(resolved(var), rejected(var)) -> promise
|
700 | #catch(rejected(var)) -> promise
|
701 | .resolve(promise | var) -> promise
|
702 | .reject(var) -> promise
|
703 | .all(iterable) -> promise
|
704 | .race(iterable) -> promise
|
705 | ```
|
706 | [*CommonJS entry points:*](#commonjs)
|
707 | ```
|
708 | core-js(/library)/es6/promise
|
709 | core-js(/library)/fn/promise
|
710 | ```
|
711 | Basic [*example*](http://goo.gl/vGrtUC):
|
712 | ```js
|
713 | function sleepRandom(time){
|
714 | return new Promise(function(resolve, reject){
|
715 | setTimeout(resolve, time * 1e3, 0 | Math.random() * 1e3);
|
716 | });
|
717 | }
|
718 |
|
719 | console.log('Run'); // => Run
|
720 | sleepRandom(5).then(function(result){
|
721 | console.log(result); // => 869, after 5 sec.
|
722 | return sleepRandom(10);
|
723 | }).then(function(result){
|
724 | console.log(result); // => 202, after 10 sec.
|
725 | }).then(function(){
|
726 | console.log('immediately after'); // => immediately after
|
727 | throw Error('Irror!');
|
728 | }).then(function(){
|
729 | console.log('will not be displayed');
|
730 | }).catch(x => console.log(x)); // => => Error: Irror!
|
731 | ```
|
732 | `Promise.resolve` and `Promise.reject` [*example*](http://goo.gl/vr8TN3):
|
733 | ```js
|
734 | Promise.resolve(42).then(x => console.log(x)); // => 42
|
735 | Promise.reject(42).catch(x => console.log(x)); // => 42
|
736 |
|
737 | Promise.resolve($.getJSON('/data.json')); // => ES6 promise
|
738 | ```
|
739 | `Promise.all` [*example*](http://goo.gl/RdoDBZ):
|
740 | ```js
|
741 | Promise.all([
|
742 | 'foo',
|
743 | sleepRandom(5),
|
744 | sleepRandom(15),
|
745 | sleepRandom(10) // after 15 sec:
|
746 | ]).then(x => console.log(x)); // => ['foo', 956, 85, 382]
|
747 | ```
|
748 | `Promise.race` [*example*](http://goo.gl/L8ovkJ):
|
749 | ```js
|
750 | function timeLimit(promise, time){
|
751 | return Promise.race([promise, new Promise(function(resolve, reject){
|
752 | setTimeout(reject, time * 1e3, Error('Await > ' + time + ' sec'));
|
753 | })]);
|
754 | }
|
755 |
|
756 | timeLimit(sleepRandom(5), 10).then(x => console.log(x)); // => 853, after 5 sec.
|
757 | timeLimit(sleepRandom(15), 10).catch(x => console.log(x)); // Error: Await > 10 sec
|
758 | ```
|
759 | ECMAScript 7 [async functions](https://tc39.github.io/ecmascript-asyncawait) [example](http://goo.gl/wnQS4j):
|
760 | ```js
|
761 | var delay = time => new Promise(resolve => setTimeout(resolve, time))
|
762 |
|
763 | async function sleepRandom(time){
|
764 | await delay(time * 1e3);
|
765 | return 0 | Math.random() * 1e3;
|
766 | };
|
767 | async function sleepError(time, msg){
|
768 | await delay(time * 1e3);
|
769 | throw Error(msg);
|
770 | };
|
771 |
|
772 | (async () => {
|
773 | try {
|
774 | console.log('Run'); // => Run
|
775 | console.log(await sleepRandom(5)); // => 936, after 5 sec.
|
776 | var [a, b, c] = await Promise.all([
|
777 | sleepRandom(5),
|
778 | sleepRandom(15),
|
779 | sleepRandom(10)
|
780 | ]);
|
781 | console.log(a, b, c); // => 210 445 71, after 15 sec.
|
782 | await sleepError(5, 'Irror!');
|
783 | console.log('Will not be displayed');
|
784 | } catch(e){
|
785 | console.log(e); // => Error: 'Irror!', after 5 sec.
|
786 | }
|
787 | })();
|
788 | ```
|
789 |
|
790 | ##### Unhandled rejection tracking
|
791 |
|
792 | In Node.js, like in native implementation, available events [`unhandledRejection`](https://nodejs.org/api/process.html#process_event_unhandledrejection) and [`rejectionHandled`](https://nodejs.org/api/process.html#process_event_rejectionhandled):
|
793 | ```js
|
794 | process.on('unhandledRejection', (reason, promise) => console.log('unhandled', reason, promise));
|
795 | process.on('rejectionHandled', (promise) => console.log('handled', promise));
|
796 |
|
797 | var p = Promise.reject(42);
|
798 | // unhandled 42 [object Promise]
|
799 |
|
800 | setTimeout(() => p.catch(_ => _), 1e3);
|
801 | // handled [object Promise]
|
802 | ```
|
803 | In a browser on rejection, by default, you will see notify in the console, or you can add a custom handler and a handler on handling unhandled, [*example*](http://goo.gl/Wozskl):
|
804 | ```js
|
805 | window.onunhandledrejection = e => console.log('unhandled', e.reason, e.promise);
|
806 | window.onrejectionhandled = e => console.log('handled', e.reason, e.promise);
|
807 |
|
808 | var p = Promise.reject(42);
|
809 | // unhandled 42 [object Promise]
|
810 |
|
811 | setTimeout(() => p.catch(_ => _), 1e3);
|
812 | // handled 42 [object Promise]
|
813 | ```
|
814 |
|
815 | #### ECMAScript 6: Symbol
|
816 | Module [`es6.symbol`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.symbol.js).
|
817 | ```js
|
818 | Symbol(description?) -> symbol
|
819 | .hasInstance -> @@hasInstance
|
820 | .isConcatSpreadable -> @@isConcatSpreadable
|
821 | .iterator -> @@iterator
|
822 | .match -> @@match
|
823 | .replace -> @@replace
|
824 | .search -> @@search
|
825 | .species -> @@species
|
826 | .split -> @@split
|
827 | .toPrimitive -> @@toPrimitive
|
828 | .toStringTag -> @@toStringTag
|
829 | .unscopables -> @@unscopables
|
830 | .for(key) -> symbol
|
831 | .keyFor(symbol) -> key
|
832 | .useSimple() -> void
|
833 | .useSetter() -> void
|
834 | Object
|
835 | .getOwnPropertySymbols(object) -> array
|
836 | ```
|
837 | Also wrapped some methods for correct work with `Symbol` polyfill.
|
838 | ```js
|
839 | Object
|
840 | .create(proto | null, descriptors?) -> object
|
841 | .defineProperty(target, key, desc) -> target
|
842 | .defineProperties(target, descriptors) -> target
|
843 | .getOwnPropertyDescriptor(var, key) -> desc | undefined
|
844 | .getOwnPropertyNames(var) -> array
|
845 | #propertyIsEnumerable(key) -> bool
|
846 | JSON
|
847 | .stringify(target, replacer?, space?) -> string | undefined
|
848 | ```
|
849 | [*CommonJS entry points:*](#commonjs)
|
850 | ```
|
851 | core-js(/library)/es6/symbol
|
852 | core-js(/library)/fn/symbol
|
853 | core-js(/library)/fn/symbol/has-instance
|
854 | core-js(/library)/fn/symbol/is-concat-spreadable
|
855 | core-js(/library)/fn/symbol/iterator
|
856 | core-js(/library)/fn/symbol/match
|
857 | core-js(/library)/fn/symbol/replace
|
858 | core-js(/library)/fn/symbol/search
|
859 | core-js(/library)/fn/symbol/species
|
860 | core-js(/library)/fn/symbol/split
|
861 | core-js(/library)/fn/symbol/to-primitive
|
862 | core-js(/library)/fn/symbol/to-string-tag
|
863 | core-js(/library)/fn/symbol/unscopables
|
864 | core-js(/library)/fn/symbol/for
|
865 | core-js(/library)/fn/symbol/key-for
|
866 | ```
|
867 | [*Basic example*](http://goo.gl/BbvWFc):
|
868 | ```js
|
869 | var Person = (function(){
|
870 | var NAME = Symbol('name');
|
871 | function Person(name){
|
872 | this[NAME] = name;
|
873 | }
|
874 | Person.prototype.getName = function(){
|
875 | return this[NAME];
|
876 | };
|
877 | return Person;
|
878 | })();
|
879 |
|
880 | var person = new Person('Vasya');
|
881 | console.log(person.getName()); // => 'Vasya'
|
882 | console.log(person['name']); // => undefined
|
883 | console.log(person[Symbol('name')]); // => undefined, symbols are uniq
|
884 | for(var key in person)console.log(key); // => only 'getName', symbols are not enumerable
|
885 | ```
|
886 | `Symbol.for` & `Symbol.keyFor` [*example*](http://goo.gl/0pdJjX):
|
887 | ```js
|
888 | var symbol = Symbol.for('key');
|
889 | symbol === Symbol.for('key'); // true
|
890 | Symbol.keyFor(symbol); // 'key'
|
891 | ```
|
892 | [*Example*](http://goo.gl/mKVOQJ) with methods for getting own object keys:
|
893 | ```js
|
894 | var O = {a: 1};
|
895 | Object.defineProperty(O, 'b', {value: 2});
|
896 | O[Symbol('c')] = 3;
|
897 | Object.keys(O); // => ['a']
|
898 | Object.getOwnPropertyNames(O); // => ['a', 'b']
|
899 | Object.getOwnPropertySymbols(O); // => [Symbol(c)]
|
900 | Reflect.ownKeys(O); // => ['a', 'b', Symbol(c)]
|
901 | ```
|
902 | ##### Caveats when using `Symbol` polyfill:
|
903 |
|
904 | * We can't add new primitive type, `Symbol` returns object.
|
905 | * `Symbol.for` and `Symbol.keyFor` can't be shimmed cross-realm.
|
906 | * By default, to hide the keys, `Symbol` polyfill defines setter in `Object.prototype`. For this reason, uncontrolled creation of symbols can cause memory leak and the `in` operator is not working correctly with `Symbol` polyfill: `Symbol() in {} // => true`.
|
907 |
|
908 | You can disable defining setters in `Object.prototype`. [Example](http://goo.gl/N5UD7J):
|
909 | ```js
|
910 | Symbol.useSimple();
|
911 | var s1 = Symbol('s1')
|
912 | , o1 = {};
|
913 | o1[s1] = true;
|
914 | for(var key in o1)console.log(key); // => 'Symbol(s1)_t.qamkg9f3q', w/o native Symbol
|
915 |
|
916 | Symbol.useSetter();
|
917 | var s2 = Symbol('s2')
|
918 | , o2 = {};
|
919 | o2[s2] = true;
|
920 | for(var key in o2)console.log(key); // nothing
|
921 | ```
|
922 | * Currently, `core-js` not adds setters to `Object.prototype` for well-known symbols for correct work something like `Symbol.iterator in foo`. It can cause problems with their enumerability.
|
923 | * Some problems possible with environment exotic objects (for example, IE `localStorage`).
|
924 |
|
925 | #### ECMAScript 6: Collections
|
926 | `core-js` uses native collections in most case, just fixes methods / constructor, if it's required, and in old environment uses fast polyfill (O(1) lookup).
|
927 | #### Map
|
928 | Module [`es6.map`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.map.js).
|
929 | ```js
|
930 | new Map(iterable (entries) ?) -> map
|
931 | #clear() -> void
|
932 | #delete(key) -> bool
|
933 | #forEach(fn(val, key, @), that) -> void
|
934 | #get(key) -> val
|
935 | #has(key) -> bool
|
936 | #set(key, val) -> @
|
937 | #size -> uint
|
938 | #values() -> iterator
|
939 | #keys() -> iterator
|
940 | #entries() -> iterator
|
941 | #@@iterator() -> iterator (entries)
|
942 | ```
|
943 | [*CommonJS entry points:*](#commonjs)
|
944 | ```
|
945 | core-js(/library)/es6/map
|
946 | core-js(/library)/fn/map
|
947 | ```
|
948 | [*Examples*](http://goo.gl/GWR7NI):
|
949 | ```js
|
950 | var a = [1];
|
951 |
|
952 | var map = new Map([['a', 1], [42, 2]]);
|
953 | map.set(a, 3).set(true, 4);
|
954 |
|
955 | console.log(map.size); // => 4
|
956 | console.log(map.has(a)); // => true
|
957 | console.log(map.has([1])); // => false
|
958 | console.log(map.get(a)); // => 3
|
959 | map.forEach(function(val, key){
|
960 | console.log(val); // => 1, 2, 3, 4
|
961 | console.log(key); // => 'a', 42, [1], true
|
962 | });
|
963 | map.delete(a);
|
964 | console.log(map.size); // => 3
|
965 | console.log(map.get(a)); // => undefined
|
966 | console.log(Array.from(map)); // => [['a', 1], [42, 2], [true, 4]]
|
967 |
|
968 | var map = new Map([['a', 1], ['b', 2], ['c', 3]]);
|
969 |
|
970 | for(var [key, val] of map){
|
971 | console.log(key); // => 'a', 'b', 'c'
|
972 | console.log(val); // => 1, 2, 3
|
973 | }
|
974 | for(var val of map.values())console.log(val); // => 1, 2, 3
|
975 | for(var key of map.keys())console.log(key); // => 'a', 'b', 'c'
|
976 | for(var [key, val] of map.entries()){
|
977 | console.log(key); // => 'a', 'b', 'c'
|
978 | console.log(val); // => 1, 2, 3
|
979 | }
|
980 | ```
|
981 | #### Set
|
982 | Module [`es6.set`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.set.js).
|
983 | ```js
|
984 | new Set(iterable?) -> set
|
985 | #add(key) -> @
|
986 | #clear() -> void
|
987 | #delete(key) -> bool
|
988 | #forEach(fn(el, el, @), that) -> void
|
989 | #has(key) -> bool
|
990 | #size -> uint
|
991 | #values() -> iterator
|
992 | #keys() -> iterator
|
993 | #entries() -> iterator
|
994 | #@@iterator() -> iterator (values)
|
995 | ```
|
996 | [*CommonJS entry points:*](#commonjs)
|
997 | ```
|
998 | core-js(/library)/es6/set
|
999 | core-js(/library)/fn/set
|
1000 | ```
|
1001 | [*Examples*](http://goo.gl/bmhLwg):
|
1002 | ```js
|
1003 | var set = new Set(['a', 'b', 'a', 'c']);
|
1004 | set.add('d').add('b').add('e');
|
1005 | console.log(set.size); // => 5
|
1006 | console.log(set.has('b')); // => true
|
1007 | set.forEach(function(it){
|
1008 | console.log(it); // => 'a', 'b', 'c', 'd', 'e'
|
1009 | });
|
1010 | set.delete('b');
|
1011 | console.log(set.size); // => 4
|
1012 | console.log(set.has('b')); // => false
|
1013 | console.log(Array.from(set)); // => ['a', 'c', 'd', 'e']
|
1014 |
|
1015 | var set = new Set([1, 2, 3, 2, 1]);
|
1016 |
|
1017 | for(var val of set)console.log(val); // => 1, 2, 3
|
1018 | for(var val of set.values())console.log(val); // => 1, 2, 3
|
1019 | for(var key of set.keys())console.log(key); // => 1, 2, 3
|
1020 | for(var [key, val] of set.entries()){
|
1021 | console.log(key); // => 1, 2, 3
|
1022 | console.log(val); // => 1, 2, 3
|
1023 | }
|
1024 | ```
|
1025 | #### WeakMap
|
1026 | Module [`es6.weak-map`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.weak-map.js).
|
1027 | ```js
|
1028 | new WeakMap(iterable (entries) ?) -> weakmap
|
1029 | #delete(key) -> bool
|
1030 | #get(key) -> val
|
1031 | #has(key) -> bool
|
1032 | #set(key, val) -> @
|
1033 | ```
|
1034 | [*CommonJS entry points:*](#commonjs)
|
1035 | ```
|
1036 | core-js(/library)/es6/weak-map
|
1037 | core-js(/library)/fn/weak-map
|
1038 | ```
|
1039 | [*Examples*](http://goo.gl/SILXyw):
|
1040 | ```js
|
1041 | var a = [1]
|
1042 | , b = [2]
|
1043 | , c = [3];
|
1044 |
|
1045 | var wmap = new WeakMap([[a, 1], [b, 2]]);
|
1046 | wmap.set(c, 3).set(b, 4);
|
1047 | console.log(wmap.has(a)); // => true
|
1048 | console.log(wmap.has([1])); // => false
|
1049 | console.log(wmap.get(a)); // => 1
|
1050 | wmap.delete(a);
|
1051 | console.log(wmap.get(a)); // => undefined
|
1052 |
|
1053 | // Private properties store:
|
1054 | var Person = (function(){
|
1055 | var names = new WeakMap;
|
1056 | function Person(name){
|
1057 | names.set(this, name);
|
1058 | }
|
1059 | Person.prototype.getName = function(){
|
1060 | return names.get(this);
|
1061 | };
|
1062 | return Person;
|
1063 | })();
|
1064 |
|
1065 | var person = new Person('Vasya');
|
1066 | console.log(person.getName()); // => 'Vasya'
|
1067 | for(var key in person)console.log(key); // => only 'getName'
|
1068 | ```
|
1069 | #### WeakSet
|
1070 | Module [`es6.weak-set`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.weak-set.js).
|
1071 | ```js
|
1072 | new WeakSet(iterable?) -> weakset
|
1073 | #add(key) -> @
|
1074 | #delete(key) -> bool
|
1075 | #has(key) -> bool
|
1076 | ```
|
1077 | [*CommonJS entry points:*](#commonjs)
|
1078 | ```
|
1079 | core-js(/library)/es6/weak-set
|
1080 | core-js(/library)/fn/weak-set
|
1081 | ```
|
1082 | [*Examples*](http://goo.gl/TdFbEx):
|
1083 | ```js
|
1084 | var a = [1]
|
1085 | , b = [2]
|
1086 | , c = [3];
|
1087 |
|
1088 | var wset = new WeakSet([a, b, a]);
|
1089 | wset.add(c).add(b).add(c);
|
1090 | console.log(wset.has(b)); // => true
|
1091 | console.log(wset.has([2])); // => false
|
1092 | wset.delete(b);
|
1093 | console.log(wset.has(b)); // => false
|
1094 | ```
|
1095 | ##### Caveats when using collections polyfill:
|
1096 |
|
1097 | * Weak-collections polyfill stores values as hidden properties of keys. It works correct and not leak in most cases. However, it is desirable to store a collection longer than its keys.
|
1098 |
|
1099 | #### ECMAScript 6: Typed Arrays
|
1100 | Implementations and fixes `ArrayBuffer`, `DataView`, typed arrays constructors, static and prototype methods. Typed Arrays work only in environments with support descriptors (IE9+), `ArrayBuffer` and `DataView` should work anywhere.
|
1101 |
|
1102 | Modules [`es6.typed.array-buffer`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.typed.array-buffer.js), [`es6.typed.data-view`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.typed.data-view.js), [`es6.typed.int8-array`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.typed.int8-array.js), [`es6.typed.uint8-array`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.typed.uint8-array.js), [`es6.typed.uint8-clamped-array`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.typed.uint8-clamped-array.js), [`es6.typed.int16-array`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.typed.int16-array.js), [`es6.typed.uint16-array`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.typed.uint16-array.js), [`es6.typed.int32-array`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.typed.int32-array.js), [`es6.typed.uint32-array`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.typed.uint32-array.js), [`es6.typed.float32-array`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.typed.float32-array.js) and [`es6.typed.float64-array`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.typed.float64-array.js).
|
1103 | ```js
|
1104 | new ArrayBuffer(length) -> buffer
|
1105 | .isView(var) -> bool
|
1106 | #slice(start = 0, end = @length) -> buffer
|
1107 | #byteLength -> uint
|
1108 |
|
1109 | new DataView(buffer, byteOffset = 0, byteLength = buffer.byteLength - byteOffset) -> view
|
1110 | #getInt8(offset) -> int8
|
1111 | #getUint8(offset) -> uint8
|
1112 | #getInt16(offset, littleEndian = false) -> int16
|
1113 | #getUint16(offset, littleEndian = false) -> uint16
|
1114 | #getInt32(offset, littleEndian = false) -> int32
|
1115 | #getUint32(offset, littleEndian = false) -> uint32
|
1116 | #getFloat32(offset, littleEndian = false) -> float32
|
1117 | #getFloat64(offset, littleEndian = false) -> float64
|
1118 | #setInt8(offset, value) -> void
|
1119 | #setUint8(offset, value) -> void
|
1120 | #setInt16(offset, value, littleEndian = false) -> void
|
1121 | #setUint16(offset, value, littleEndian = false) -> void
|
1122 | #setInt32(offset, value, littleEndian = false) -> void
|
1123 | #setUint32(offset, value, littleEndian = false) -> void
|
1124 | #setFloat32(offset, value, littleEndian = false) -> void
|
1125 | #setFloat64(offset, value, littleEndian = false) -> void
|
1126 | #buffer -> buffer
|
1127 | #byteLength -> uint
|
1128 | #byteOffset -> uint
|
1129 |
|
1130 | {
|
1131 | Int8Array,
|
1132 | Uint8Array,
|
1133 | Uint8ClampedArray,
|
1134 | Int16Array,
|
1135 | Uint16Array,
|
1136 | Int32Array,
|
1137 | Uint32Array,
|
1138 | Float32Array,
|
1139 | Float64Array
|
1140 | }
|
1141 | new %TypedArray%(length) -> typed
|
1142 | new %TypedArray%(typed) -> typed
|
1143 | new %TypedArray%(arrayLike) -> typed
|
1144 | new %TypedArray%(iterable) -> typed
|
1145 | new %TypedArray%(buffer, byteOffset = 0, length = (buffer.byteLength - byteOffset) / @BYTES_PER_ELEMENT) -> typed
|
1146 | .BYTES_PER_ELEMENT -> uint
|
1147 | .from(arrayLike | iterable, mapFn(val, index)?, that) -> typed
|
1148 | .of(...args) -> typed
|
1149 | #BYTES_PER_ELEMENT -> uint
|
1150 | #copyWithin(target = 0, start = 0, end = @length) -> @
|
1151 | #every(fn(val, index, @), that) -> bool
|
1152 | #fill(val, start = 0, end = @length) -> @
|
1153 | #filter(fn(val, index, @), that) -> typed
|
1154 | #find(fn(val, index, @), that) -> val
|
1155 | #findIndex(fn(val, index, @), that) -> index
|
1156 | #forEach(fn(val, index, @), that) -> void
|
1157 | #indexOf(var, from?) -> int
|
1158 | #join(string = ',') -> string
|
1159 | #lastIndexOf(var, from?) -> int
|
1160 | #map(fn(val, index, @), that) -> typed
|
1161 | #reduce(fn(memo, val, index, @), memo?) -> var
|
1162 | #reduceRight(fn(memo, val, index, @), memo?) -> var
|
1163 | #reverse() -> @
|
1164 | #set(arrayLike, offset = 0) -> void
|
1165 | #slice(start = 0, end = @length) -> typed
|
1166 | #some(fn(val, index, @), that) -> bool
|
1167 | #sort(fn(a, b)?) -> @
|
1168 | #subarray(start = 0, end = @length) -> typed
|
1169 | #toString() -> string
|
1170 | #toLocaleString() -> string
|
1171 | #values() -> iterator
|
1172 | #keys() -> iterator
|
1173 | #entries() -> iterator
|
1174 | #@@iterator() -> iterator (values)
|
1175 | #buffer -> buffer
|
1176 | #byteLength -> uint
|
1177 | #byteOffset -> uint
|
1178 | #length -> uint
|
1179 | ```
|
1180 | [*CommonJS entry points:*](#commonjs)
|
1181 | ```
|
1182 | core-js(/library)/es6/typed
|
1183 | core-js(/library)/fn/typed
|
1184 | core-js(/library)/fn/typed/array-buffer
|
1185 | core-js(/library)/fn/typed/data-view
|
1186 | core-js(/library)/fn/typed/int8-array
|
1187 | core-js(/library)/fn/typed/uint8-array
|
1188 | core-js(/library)/fn/typed/uint8-clamped-array
|
1189 | core-js(/library)/fn/typed/int16-array
|
1190 | core-js(/library)/fn/typed/uint16-array
|
1191 | core-js(/library)/fn/typed/int32-array
|
1192 | core-js(/library)/fn/typed/uint32-array
|
1193 | core-js(/library)/fn/typed/float32-array
|
1194 | core-js(/library)/fn/typed/float64-array
|
1195 | ```
|
1196 | [*Examples*](http://goo.gl/yla75z):
|
1197 | ```js
|
1198 | new Int32Array(4); // => [0, 0, 0, 0]
|
1199 | new Uint8ClampedArray([1, 2, 3, 666]); // => [1, 2, 3, 255]
|
1200 | new Float32Array(new Set([1, 2, 3, 2, 1])); // => [1, 2, 3]
|
1201 |
|
1202 | var buffer = new ArrayBuffer(8);
|
1203 | var view = new DataView(buffer);
|
1204 | view.setFloat64(0, 123.456, true);
|
1205 | new Uint8Array(buffer.slice(4)); // => [47, 221, 94, 64]
|
1206 |
|
1207 | Int8Array.of(1, 1.5, 5.7, 745); // => [1, 1, 5, -23]
|
1208 | Uint8Array.from([1, 1.5, 5.7, 745]); // => [1, 1, 5, 233]
|
1209 |
|
1210 | var typed = new Uint8Array([1, 2, 3]);
|
1211 |
|
1212 | var a = typed.slice(1); // => [2, 3]
|
1213 | typed.buffer === a.buffer; // => false
|
1214 | var b = typed.subarray(1); // => [2, 3]
|
1215 | typed.buffer === b.buffer; // => true
|
1216 |
|
1217 | typed.filter(it => it % 2); // => [1, 3]
|
1218 | typed.map(it => it * 1.5); // => [1, 3, 4]
|
1219 |
|
1220 | for(var val of typed)console.log(val); // => 1, 2, 3
|
1221 | for(var val of typed.values())console.log(val); // => 1, 2, 3
|
1222 | for(var key of typed.keys())console.log(key); // => 0, 1, 2
|
1223 | for(var [key, val] of typed.entries()){
|
1224 | console.log(key); // => 0, 1, 2
|
1225 | console.log(val); // => 1, 2, 3
|
1226 | }
|
1227 | ```
|
1228 | ##### Caveats when using typed arrays:
|
1229 |
|
1230 | * Typed Arrays polyfills works completely how should work by the spec, but because of internal use getter / setters on each instance, is slow and consumes significant memory. However, typed arrays polyfills required mainly for IE9 (and for `Uint8ClampedArray` in IE10 and early IE11), all modern engines have native typed arrays and requires only constructors fixes and methods.
|
1231 | * The current version hasn't special entry points for methods, they can be added only with constructors. It can be added in the future.
|
1232 | * In the `library` version we can't pollute native prototypes, so prototype methods available as constructors static.
|
1233 |
|
1234 | #### ECMAScript 6: Reflect
|
1235 | Modules [`es6.reflect.apply`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.reflect.apply.js), [`es6.reflect.construct`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.reflect.construct.js), [`es6.reflect.define-property`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.reflect.define-property.js), [`es6.reflect.delete-property`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.reflect.delete-property.js), [`es6.reflect.enumerate`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.reflect.enumerate.js), [`es6.reflect.get`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.reflect.get.js), [`es6.reflect.get-own-property-descriptor`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.reflect.get-own-property-descriptor.js), [`es6.reflect.get-prototype-of`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.reflect.get-prototype-of.js), [`es6.reflect.has`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.reflect.has.js), [`es6.reflect.is-extensible`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.reflect.is-extensible.js), [`es6.reflect.own-keys`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.reflect.own-keys.js), [`es6.reflect.prevent-extensions`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.reflect.prevent-extensions.js), [`es6.reflect.set`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.reflect.set.js), [`es6.reflect.set-prototype-of`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es6.reflect.set-prototype-of.js).
|
1236 | ```js
|
1237 | Reflect
|
1238 | .apply(target, thisArgument, argumentsList) -> var
|
1239 | .construct(target, argumentsList, newTarget?) -> object
|
1240 | .defineProperty(target, propertyKey, attributes) -> bool
|
1241 | .deleteProperty(target, propertyKey) -> bool
|
1242 | .enumerate(target) -> iterator (removed from the spec and will be removed from core-js@3)
|
1243 | .get(target, propertyKey, receiver?) -> var
|
1244 | .getOwnPropertyDescriptor(target, propertyKey) -> desc
|
1245 | .getPrototypeOf(target) -> object | null
|
1246 | .has(target, propertyKey) -> bool
|
1247 | .isExtensible(target) -> bool
|
1248 | .ownKeys(target) -> array
|
1249 | .preventExtensions(target) -> bool
|
1250 | .set(target, propertyKey, V, receiver?) -> bool
|
1251 | .setPrototypeOf(target, proto) -> bool (required __proto__ - IE11+)
|
1252 | ```
|
1253 | [*CommonJS entry points:*](#commonjs)
|
1254 | ```
|
1255 | core-js(/library)/es6/reflect
|
1256 | core-js(/library)/fn/reflect
|
1257 | core-js(/library)/fn/reflect/apply
|
1258 | core-js(/library)/fn/reflect/construct
|
1259 | core-js(/library)/fn/reflect/define-property
|
1260 | core-js(/library)/fn/reflect/delete-property
|
1261 | core-js(/library)/fn/reflect/enumerate (deprecated and will be removed from the next major release)
|
1262 | core-js(/library)/fn/reflect/get
|
1263 | core-js(/library)/fn/reflect/get-own-property-descriptor
|
1264 | core-js(/library)/fn/reflect/get-prototype-of
|
1265 | core-js(/library)/fn/reflect/has
|
1266 | core-js(/library)/fn/reflect/is-extensible
|
1267 | core-js(/library)/fn/reflect/own-keys
|
1268 | core-js(/library)/fn/reflect/prevent-extensions
|
1269 | core-js(/library)/fn/reflect/set
|
1270 | core-js(/library)/fn/reflect/set-prototype-of
|
1271 | ```
|
1272 | [*Examples*](http://goo.gl/gVT0cH):
|
1273 | ```js
|
1274 | var O = {a: 1};
|
1275 | Object.defineProperty(O, 'b', {value: 2});
|
1276 | O[Symbol('c')] = 3;
|
1277 | Reflect.ownKeys(O); // => ['a', 'b', Symbol(c)]
|
1278 |
|
1279 | function C(a, b){
|
1280 | this.c = a + b;
|
1281 | }
|
1282 |
|
1283 | var instance = Reflect.construct(C, [20, 22]);
|
1284 | instance.c; // => 42
|
1285 | ```
|
1286 |
|
1287 | ### ECMAScript 7+ proposals
|
1288 | [The TC39 process.](https://tc39.github.io/process-document/)
|
1289 |
|
1290 | [*CommonJS entry points:*](#commonjs)
|
1291 | ```
|
1292 | core-js(/library)/es7
|
1293 | core-js(/library)/es7/array
|
1294 | core-js(/library)/es7/global
|
1295 | core-js(/library)/es7/string
|
1296 | core-js(/library)/es7/map
|
1297 | core-js(/library)/es7/set
|
1298 | core-js(/library)/es7/error
|
1299 | core-js(/library)/es7/math
|
1300 | core-js(/library)/es7/system
|
1301 | core-js(/library)/es7/symbol
|
1302 | core-js(/library)/es7/reflect
|
1303 | core-js(/library)/es7/observable
|
1304 | ```
|
1305 | `core-js/stage/4` entry point contains only stage 4 proposals, `core-js/stage/3` - stage 3 and stage 4, etc.
|
1306 | #### Stage 4 proposals
|
1307 |
|
1308 | [*CommonJS entry points:*](#commonjs)
|
1309 | ```js
|
1310 | core-js(/library)/stage/4
|
1311 | ```
|
1312 | * `{Array, %TypedArray%}#includes` [proposal](https://github.com/tc39/Array.prototype.includes) - module [`es7.array.includes`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.array.includes.js), `%TypedArray%` version in modules from [this section](#ecmascript-6-typed-arrays).
|
1313 | ```js
|
1314 | Array
|
1315 | #includes(var, from?) -> bool
|
1316 | {
|
1317 | Int8Array,
|
1318 | Uint8Array,
|
1319 | Uint8ClampedArray,
|
1320 | Int16Array,
|
1321 | Uint16Array,
|
1322 | Int32Array,
|
1323 | Uint32Array,
|
1324 | Float32Array,
|
1325 | Float64Array
|
1326 | }
|
1327 | #includes(var, from?) -> bool
|
1328 | ```
|
1329 | [*CommonJS entry points:*](#commonjs)
|
1330 | ```js
|
1331 | core-js(/library)/fn/array/includes
|
1332 | ```
|
1333 | [*Examples*](http://goo.gl/2Gq4ma):
|
1334 | ```js
|
1335 | [1, 2, 3].includes(2); // => true
|
1336 | [1, 2, 3].includes(4); // => false
|
1337 | [1, 2, 3].includes(2, 2); // => false
|
1338 |
|
1339 | [NaN].indexOf(NaN); // => -1
|
1340 | [NaN].includes(NaN); // => true
|
1341 | Array(1).indexOf(undefined); // => -1
|
1342 | Array(1).includes(undefined); // => true
|
1343 | ```
|
1344 | * `Object.values`, `Object.entries` [proposal](https://github.com/tc39/proposal-object-values-entries) - modules [`es7.object.values`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.object.values.js), [`es7.object.entries`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.object.entries.js)
|
1345 | ```js
|
1346 | Object
|
1347 | .values(object) -> array
|
1348 | .entries(object) -> array
|
1349 | ```
|
1350 | [*CommonJS entry points:*](#commonjs)
|
1351 | ```js
|
1352 | core-js(/library)/fn/object/values
|
1353 | core-js(/library)/fn/object/entries
|
1354 | ```
|
1355 | [*Examples*](http://goo.gl/6kuGOn):
|
1356 | ```js
|
1357 | Object.values({a: 1, b: 2, c: 3}); // => [1, 2, 3]
|
1358 | Object.entries({a: 1, b: 2, c: 3}); // => [['a', 1], ['b', 2], ['c', 3]]
|
1359 |
|
1360 | for(let [key, value] of Object.entries({a: 1, b: 2, c: 3})){
|
1361 | console.log(key); // => 'a', 'b', 'c'
|
1362 | console.log(value); // => 1, 2, 3
|
1363 | }
|
1364 | ```
|
1365 | * `Object.getOwnPropertyDescriptors` [proposal](https://github.com/tc39/proposal-object-getownpropertydescriptors) - module [`es7.object.get-own-property-descriptors`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.object.get-own-property-descriptors.js)
|
1366 | ```js
|
1367 | Object
|
1368 | .getOwnPropertyDescriptors(object) -> object
|
1369 | ```
|
1370 | [*CommonJS entry points:*](#commonjs)
|
1371 | ```js
|
1372 | core-js(/library)/fn/object/get-own-property-descriptors
|
1373 | ```
|
1374 | *Examples*:
|
1375 | ```js
|
1376 | // Shallow object cloning with prototype and descriptors:
|
1377 | var copy = Object.create(Object.getPrototypeOf(O), Object.getOwnPropertyDescriptors(O));
|
1378 | // Mixin:
|
1379 | Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
|
1380 | ```
|
1381 | * `String#padStart`, `String#padEnd` [proposal](https://github.com/tc39/proposal-string-pad-start-end) - modules [`es7.string.pad-start`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.string.pad-start.js), [`es7.string.pad-end`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.string.pad-end.js)
|
1382 | ```js
|
1383 | String
|
1384 | #padStart(length, fillStr = ' ') -> string
|
1385 | #padEnd(length, fillStr = ' ') -> string
|
1386 | ```
|
1387 | [*CommonJS entry points:*](#commonjs)
|
1388 | ```js
|
1389 | core-js(/library)/fn/string/pad-start
|
1390 | core-js(/library)/fn/string/pad-end
|
1391 | core-js(/library)/fn/string/virtual/pad-start
|
1392 | core-js(/library)/fn/string/virtual/pad-end
|
1393 | ```
|
1394 | [*Examples*](http://goo.gl/hK5ccv):
|
1395 | ```js
|
1396 | 'hello'.padStart(10); // => ' hello'
|
1397 | 'hello'.padStart(10, '1234'); // => '12341hello'
|
1398 | 'hello'.padEnd(10); // => 'hello '
|
1399 | 'hello'.padEnd(10, '1234'); // => 'hello12341'
|
1400 | ```
|
1401 | * `Object#__(define|lookup)[GS]etter__`, [annex B ES2017](https://github.com/tc39/ecma262/pull/381), but we haven't special namespace for that - modules [`es7.object.define-setter`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.object.define-setter.js), [`es7.object.define-getter`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.object.define-getter.js), [`es7.object.lookup-setter`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.object.lookup-setter.js) and [`es7.object.lookup-getter`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.object.lookup-getter.js).
|
1402 | ```js
|
1403 | Object
|
1404 | #__defineSetter__(key, fn) -> void
|
1405 | #__defineGetter__(key, fn) -> void
|
1406 | #__lookupSetter__(key) -> fn | void
|
1407 | #__lookupGetter__(key) -> fn | void
|
1408 | ```
|
1409 | [*CommonJS entry points:*](#commonjs)
|
1410 | ```js
|
1411 | core-js(/library)/fn/object/define-getter
|
1412 | core-js(/library)/fn/object/define-setter
|
1413 | core-js(/library)/fn/object/lookup-getter
|
1414 | core-js(/library)/fn/object/lookup-setter
|
1415 | ```
|
1416 |
|
1417 | #### Stage 3 proposals
|
1418 | [*CommonJS entry points:*](#commonjs)
|
1419 | ```js
|
1420 | core-js(/library)/stage/3
|
1421 | ```
|
1422 | * `global` [proposal](https://github.com/tc39/proposal-global) - modules [`es7.global`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.global.js) and [`es7.system.global`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.system.global.js) (obsolete)
|
1423 | ```js
|
1424 | global -> object
|
1425 | System
|
1426 | .global -> object (obsolete)
|
1427 | ```
|
1428 | [*CommonJS entry points:*](#commonjs)
|
1429 | ```js
|
1430 | core-js(/library)/fn/global
|
1431 | core-js(/library)/fn/system/global (obsolete)
|
1432 | ```
|
1433 | [*Examples*](http://goo.gl/gEqMl7):
|
1434 | ```js
|
1435 | global.Array === Array; // => true
|
1436 | ```
|
1437 | * `Promise#finally` [proposal](https://github.com/tc39/proposal-promise-finally) - module [`es7.promise.finally`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.promise.finally.js)
|
1438 | ```js
|
1439 | Promise
|
1440 | #finally(onFinally()) -> promise
|
1441 | ```
|
1442 | [*CommonJS entry points:*](#commonjs)
|
1443 | ```js
|
1444 | core-js(/library)/fn/promise/finally
|
1445 | ```
|
1446 | [*Examples*](https://goo.gl/AhyBbJ):
|
1447 | ```js
|
1448 | Promise.resolve(42).finally(() => console.log('You will see it anyway'));
|
1449 |
|
1450 | Promise.reject(42).finally(() => console.log('You will see it anyway'));
|
1451 |
|
1452 | #### Stage 2 proposals
|
1453 | [*CommonJS entry points:*](#commonjs)
|
1454 | ```js
|
1455 | core-js(/library)/stage/2
|
1456 | ```
|
1457 | * `String#trimLeft`, `String#trimRight` / `String#trimStart`, `String#trimEnd` [proposal](https://github.com/sebmarkbage/ecmascript-string-left-right-trim) - modules [`es7.string.trim-left`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.string.trim-right.js), [`es7.string.trim-right`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.string.trim-right.js)
|
1458 | ```js
|
1459 | String
|
1460 | #trimLeft() -> string
|
1461 | #trimRight() -> string
|
1462 | #trimStart() -> string
|
1463 | #trimEnd() -> string
|
1464 | ```
|
1465 | [*CommonJS entry points:*](#commonjs)
|
1466 | ```js
|
1467 | core-js(/library)/fn/string/trim-start
|
1468 | core-js(/library)/fn/string/trim-end
|
1469 | core-js(/library)/fn/string/trim-left
|
1470 | core-js(/library)/fn/string/trim-right
|
1471 | core-js(/library)/fn/string/virtual/trim-start
|
1472 | core-js(/library)/fn/string/virtual/trim-end
|
1473 | core-js(/library)/fn/string/virtual/trim-left
|
1474 | core-js(/library)/fn/string/virtual/trim-right
|
1475 | ```
|
1476 | [*Examples*](http://goo.gl/Er5lMJ):
|
1477 | ```js
|
1478 | ' hello '.trimLeft(); // => 'hello '
|
1479 | ' hello '.trimRight(); // => ' hello'
|
1480 | ```
|
1481 | ```
|
1482 | * `Symbol.asyncIterator` for [async iteration proposal](https://github.com/tc39/proposal-async-iteration) - module [`es7.symbol.async-iterator`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.symbol.async-iterator.js)
|
1483 | ```js
|
1484 | Symbol
|
1485 | .asyncIterator -> @@asyncIterator
|
1486 | ```
|
1487 | [*CommonJS entry points:*](#commonjs)
|
1488 | ```js
|
1489 | core-js(/library)/fn/symbol/async-iterator
|
1490 | ```
|
1491 |
|
1492 | #### Stage 1 proposals
|
1493 | [*CommonJS entry points:*](#commonjs)
|
1494 | ```js
|
1495 | core-js(/library)/stage/1
|
1496 | ```
|
1497 | * `Promise.try` [proposal](https://github.com/tc39/proposal-promise-try) - module [`es7.promise.try`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.promise.try.js)
|
1498 | ```js
|
1499 | Promise
|
1500 | .try(function()) -> promise
|
1501 | ```
|
1502 | [*CommonJS entry points:*](#commonjs)
|
1503 | ```js
|
1504 | core-js(/library)/fn/promise/try
|
1505 | ```
|
1506 | [*Examples*](https://goo.gl/k5GGRo):
|
1507 | ```js
|
1508 | Promise.try(() => 42).then(it => console.log(`Promise, resolved as ${it}`));
|
1509 |
|
1510 | Promise.try(() => { throw 42; }).catch(it => console.log(`Promise, rejected as ${it}`));
|
1511 | ```
|
1512 | * `Array#flatten` and `Array#flatMap` [proposal](https://tc39.github.io/proposal-flatMap) - modules [`es7.array.flatten`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.array.flatten.js) and [`es7.array.flat-map`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.array.flat-map.js)
|
1513 | ```js
|
1514 | Array
|
1515 | #flatten(depthArg = 1) -> array
|
1516 | #flatMap(fn(val, key, @), that) -> array
|
1517 | ```
|
1518 | [*CommonJS entry points:*](#commonjs)
|
1519 | ```js
|
1520 | core-js(/library)/fn/array/flatten
|
1521 | core-js(/library)/fn/array/flat-map
|
1522 | core-js(/library)/fn/array/virtual/flatten
|
1523 | core-js(/library)/fn/array/virtual/flat-map
|
1524 | ```
|
1525 | [*Examples*](https://goo.gl/jTXsZi):
|
1526 | ```js
|
1527 | [1, [2, 3], [4, 5]].flatten(); // => [1, 2, 3, 4, 5]
|
1528 | [1, [2, [3, [4]]], 5].flatten(); // => [1, 2, [3, [4]], 5]
|
1529 | [1, [2, [3, [4]]], 5].flatten(3); // => [1, 2, 3, 4, 5]
|
1530 |
|
1531 | [{a: 1, b: 2}, {a: 3, b: 4}, {a: 5, b: 6}].flatMap(it => [it.a, it.b]); // => [1, 2, 3, 4, 5, 6]
|
1532 | ```
|
1533 | * `.of` and `.from` methods on collection constructors [proposal](https://github.com/tc39/proposal-setmap-offrom) - modules [`es7.set.of`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.set.of.js), [`es7.set.from`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.set.from.js), [`es7.map.of`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.map.of.js), [`es7.map.from`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.map.from.js), [`es7.weak-set.of`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.weak-set.of.js), [`es7.weak-set.from`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.weak-set.from.js), [`es7.weak-map.of`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.weak-map.of.js), [`es7.weak-map.from`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.weak-map.from.js)
|
1534 | ```js
|
1535 | Set
|
1536 | .of(...args) -> set
|
1537 | .from(iterable, mapFn(val, index)?, that?) -> set
|
1538 | Map
|
1539 | .of(...args) -> map
|
1540 | .from(iterable, mapFn(val, index)?, that?) -> map
|
1541 | WeakSet
|
1542 | .of(...args) -> weakset
|
1543 | .from(iterable, mapFn(val, index)?, that?) -> weakset
|
1544 | WeakMap
|
1545 | .of(...args) -> weakmap
|
1546 | .from(iterable, mapFn(val, index)?, that?) -> weakmap
|
1547 | ```
|
1548 | [*CommonJS entry points:*](#commonjs)
|
1549 | ```js
|
1550 | core-js(/library)/fn/set/of
|
1551 | core-js(/library)/fn/set/from
|
1552 | core-js(/library)/fn/map/of
|
1553 | core-js(/library)/fn/map/from
|
1554 | core-js(/library)/fn/weak-set/of
|
1555 | core-js(/library)/fn/weak-set/from
|
1556 | core-js(/library)/fn/weak-map/of
|
1557 | core-js(/library)/fn/weak-map/from
|
1558 | ```
|
1559 | [*Examples*](https://goo.gl/mSC7eU):
|
1560 | ```js
|
1561 | Set.of(1, 2, 3, 2, 1); // => Set {1, 2, 3}
|
1562 |
|
1563 | Map.from([[1, 2], [3, 4]], ([key, val]) => [key ** 2, val ** 2]); // => Map {1: 4, 9: 16}
|
1564 | ```
|
1565 | * `String#matchAll` [proposal](https://github.com/tc39/String.prototype.matchAll) - module [`es7.string.match-all`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.string.match-all.js)
|
1566 | ```js
|
1567 | String
|
1568 | #matchAll(regexp) -> iterator
|
1569 | ```
|
1570 | [*CommonJS entry points:*](#commonjs)
|
1571 | ```js
|
1572 | core-js(/library)/fn/string/match-all
|
1573 | core-js(/library)/fn/string/virtual/match-all
|
1574 | ```
|
1575 | [*Examples*](http://goo.gl/6kp9EB):
|
1576 | ```js
|
1577 | for(let [_, d, D] of '1111a2b3cccc'.matchAll(/(\d)(\D)/)){
|
1578 | console.log(d, D); // => 1 a, 2 b, 3 c
|
1579 | }
|
1580 | ```
|
1581 | * `Observable` [proposal](https://github.com/zenparsing/es-observable) - modules [`es7.observable`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.observable.js) and [`es7.symbol.observable`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.symbol.observable.js)
|
1582 | ```js
|
1583 | new Observable(fn) -> observable
|
1584 | #subscribe(observer) -> subscription
|
1585 | #forEach(fn) -> promise
|
1586 | #@@observable() -> @
|
1587 | .of(...items) -> observable
|
1588 | .from(observable | iterable) -> observable
|
1589 | .@@species -> @
|
1590 | Symbol
|
1591 | .observable -> @@observable
|
1592 | ```
|
1593 | [*CommonJS entry points:*](#commonjs)
|
1594 | ```js
|
1595 | core-js(/library)/fn/observable
|
1596 | core-js(/library)/fn/symbol/observable
|
1597 | ```
|
1598 | [*Examples*](http://goo.gl/1LDywi):
|
1599 | ```js
|
1600 | new Observable(observer => {
|
1601 | observer.next('hello');
|
1602 | observer.next('world');
|
1603 | observer.complete();
|
1604 | }).forEach(it => console.log(it))
|
1605 | .then(_ => console.log('!'));
|
1606 | ```
|
1607 | * `Math.{clamp, DEG_PER_RAD, degrees, fscale, rad-per-deg, radians, scale}`
|
1608 | [proposal](https://github.com/rwaldron/proposal-math-extensions) - modules
|
1609 | [`es7.math.clamp`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.math.clamp.js),
|
1610 | [`es7.math.DEG_PER_RAD`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.math.DEG_PER_RAD.js),
|
1611 | [`es7.math.degrees`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.math.degrees.js),
|
1612 | [`es7.math.fscale`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.math.fscale.js),
|
1613 | [`es7.math.RAD_PER_DEG`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.math.RAD_PER_DEG.js),
|
1614 | [`es7.math.radians`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.math.radians.js) and
|
1615 | [`es7.math.scale`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.math.scale.js)
|
1616 | ```js
|
1617 | Math
|
1618 | .DEG_PER_RAD -> number
|
1619 | .RAD_PER_DEG -> number
|
1620 | .clamp(x, lower, upper) -> number
|
1621 | .degrees(radians) -> number
|
1622 | .fscale(x, inLow, inHigh, outLow, outHigh) -> number
|
1623 | .radians(degrees) -> number
|
1624 | .scale(x, inLow, inHigh, outLow, outHigh) -> number
|
1625 | ```
|
1626 | [*CommonJS entry points:*](#commonjs)
|
1627 | ```js
|
1628 | core-js(/library)/fn/math/clamp
|
1629 | core-js(/library)/fn/math/deg-per-rad
|
1630 | core-js(/library)/fn/math/degrees
|
1631 | core-js(/library)/fn/math/fscale
|
1632 | core-js(/library)/fn/math/rad-per-deg
|
1633 | core-js(/library)/fn/math/radians
|
1634 | core-js(/library)/fn/math/scale
|
1635 | ```
|
1636 | * `Math.signbit` [proposal](http://jfbastien.github.io/papers/Math.signbit.html) - module [`es7.math.signbit`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.math.signbit.js)
|
1637 | ```js
|
1638 | Math
|
1639 | .signbit(x) -> bool
|
1640 | ```
|
1641 | [*CommonJS entry points:*](#commonjs)
|
1642 | ```js
|
1643 | core-js(/library)/fn/math/signbit
|
1644 | ```
|
1645 | [*Examples*](http://es6.zloirock.ru/):
|
1646 | ```js
|
1647 | Math.signbit(NaN); // => NaN
|
1648 | Math.signbit(1); // => true
|
1649 | Math.signbit(-1); // => false
|
1650 | Math.signbit(0); // => true
|
1651 | Math.signbit(-0); // => false
|
1652 | ```
|
1653 |
|
1654 | #### Stage 0 proposals
|
1655 | [*CommonJS entry points:*](#commonjs)
|
1656 | ```js
|
1657 | core-js(/library)/stage/0
|
1658 | ```
|
1659 | * `String#at` [proposal](https://github.com/mathiasbynens/String.prototype.at) - module [`es7.string.at`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.string.at.js)
|
1660 | ```js
|
1661 | String
|
1662 | #at(index) -> string
|
1663 | ```
|
1664 | [*CommonJS entry points:*](#commonjs)
|
1665 | ```js
|
1666 | core-js(/library)/fn/string/at
|
1667 | core-js(/library)/fn/string/virtual/at
|
1668 | ```
|
1669 | [*Examples*](http://goo.gl/XluXI8):
|
1670 | ```js
|
1671 | 'a𠮷b'.at(1); // => '𠮷'
|
1672 | 'a𠮷b'.at(1).length; // => 2
|
1673 | ```
|
1674 | * `Map#toJSON`, `Set#toJSON` [proposal](https://github.com/DavidBruant/Map-Set.prototype.toJSON) - modules [`es7.map.to-json`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.map.to-json.js), [`es7.set.to-json`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.set.to-json.js) (rejected and will be removed from `core-js@3`)
|
1675 | ```js
|
1676 | Map
|
1677 | #toJSON() -> array (rejected and will be removed from core-js@3)
|
1678 | Set
|
1679 | #toJSON() -> array (rejected and will be removed from core-js@3)
|
1680 | ```
|
1681 | [*CommonJS entry points:*](#commonjs)
|
1682 | ```js
|
1683 | core-js(/library)/fn/map
|
1684 | core-js(/library)/fn/set
|
1685 | ```
|
1686 | * `Error.isError` [proposal](https://github.com/ljharb/proposal-is-error) - module [`es7.error.is-error`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.error.is-error.js) (withdrawn and will be removed from `core-js@3`)
|
1687 | ```js
|
1688 | Error
|
1689 | .isError(it) -> bool (withdrawn and will be removed from core-js@3)
|
1690 | ```
|
1691 | [*CommonJS entry points:*](#commonjs)
|
1692 | ```js
|
1693 | core-js(/library)/fn/error/is-error
|
1694 | ```
|
1695 | * `Math.{iaddh, isubh, imulh, umulh}` [proposal](https://gist.github.com/BrendanEich/4294d5c212a6d2254703) - modules [`es7.math.iaddh`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.math.iaddh.js), [`es7.math.isubh`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.math.isubh.js), [`es7.math.imulh`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.math.imulh.js) and [`es7.math.umulh`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.math.umulh.js)
|
1696 | ```js
|
1697 | Math
|
1698 | .iaddh(lo0, hi0, lo1, hi1) -> int32
|
1699 | .isubh(lo0, hi0, lo1, hi1) -> int32
|
1700 | .imulh(a, b) -> int32
|
1701 | .umulh(a, b) -> uint32
|
1702 | ```
|
1703 | [*CommonJS entry points:*](#commonjs)
|
1704 | ```js
|
1705 | core-js(/library)/fn/math/iaddh
|
1706 | core-js(/library)/fn/math/isubh
|
1707 | core-js(/library)/fn/math/imulh
|
1708 | core-js(/library)/fn/math/umulh
|
1709 | ```
|
1710 | * `global.asap`, [TC39 discussion](https://github.com/rwaldron/tc39-notes/blob/master/es6/2014-09/sept-25.md#510-globalasap-for-enqueuing-a-microtask), module [`es7.asap`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.asap.js)
|
1711 | ```js
|
1712 | asap(fn) -> void
|
1713 | ```
|
1714 | [*CommonJS entry points:*](#commonjs)
|
1715 | ```js
|
1716 | core-js(/library)/fn/asap
|
1717 | ```
|
1718 | [*Examples*](http://goo.gl/tx3SRK):
|
1719 | ```js
|
1720 | asap(() => console.log('called as microtask'));
|
1721 | ```
|
1722 |
|
1723 | #### Pre-stage 0 proposals
|
1724 | [*CommonJS entry points:*](#commonjs)
|
1725 | ```js
|
1726 | core-js(/library)/stage/pre
|
1727 | ```
|
1728 | * `Reflect` metadata [proposal](https://github.com/jonathandturner/decorators/blob/master/specs/metadata.md) - modules [`es7.reflect.define-metadata`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.reflect.define-metadata.js), [`es7.reflect.delete-metadata`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.reflect.delete-metadata.js), [`es7.reflect.get-metadata`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.reflect.get-metadata.js), [`es7.reflect.get-metadata-keys`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.reflect.get-metadata-keys.js), [`es7.reflect.get-own-metadata`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.reflect.get-own-metadata.js), [`es7.reflect.get-own-metadata-keys`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.reflect.get-own-metadata-keys.js), [`es7.reflect.has-metadata`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.reflect.has-metadata.js), [`es7.reflect.has-own-metadata`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.reflect.has-own-metadata.js) and [`es7.reflect.metadata`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/es7.reflect.metadata.js).
|
1729 | ```js
|
1730 | Reflect
|
1731 | .defineMetadata(metadataKey, metadataValue, target, propertyKey?) -> void
|
1732 | .getMetadata(metadataKey, target, propertyKey?) -> var
|
1733 | .getOwnMetadata(metadataKey, target, propertyKey?) -> var
|
1734 | .hasMetadata(metadataKey, target, propertyKey?) -> bool
|
1735 | .hasOwnMetadata(metadataKey, target, propertyKey?) -> bool
|
1736 | .deleteMetadata(metadataKey, target, propertyKey?) -> bool
|
1737 | .getMetadataKeys(target, propertyKey?) -> array
|
1738 | .getOwnMetadataKeys(target, propertyKey?) -> array
|
1739 | .metadata(metadataKey, metadataValue) -> decorator(target, targetKey?) -> void
|
1740 | ```
|
1741 | [*CommonJS entry points:*](#commonjs)
|
1742 | ```js
|
1743 | core-js(/library)/fn/reflect/define-metadata
|
1744 | core-js(/library)/fn/reflect/delete-metadata
|
1745 | core-js(/library)/fn/reflect/get-metadata
|
1746 | core-js(/library)/fn/reflect/get-metadata-keys
|
1747 | core-js(/library)/fn/reflect/get-own-metadata
|
1748 | core-js(/library)/fn/reflect/get-own-metadata-keys
|
1749 | core-js(/library)/fn/reflect/has-metadata
|
1750 | core-js(/library)/fn/reflect/has-own-metadata
|
1751 | core-js(/library)/fn/reflect/metadata
|
1752 | ```
|
1753 | [*Examples*](http://goo.gl/KCo3PS):
|
1754 | ```js
|
1755 | var O = {};
|
1756 | Reflect.defineMetadata('foo', 'bar', O);
|
1757 | Reflect.ownKeys(O); // => []
|
1758 | Reflect.getOwnMetadataKeys(O); // => ['foo']
|
1759 | Reflect.getOwnMetadata('foo', O); // => 'bar'
|
1760 | ```
|
1761 |
|
1762 | ### Web standards
|
1763 | [*CommonJS entry points:*](#commonjs)
|
1764 | ```js
|
1765 | core-js(/library)/web
|
1766 | ```
|
1767 | #### setTimeout / setInterval
|
1768 | Module [`web.timers`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/web.timers.js). Additional arguments fix for IE9-.
|
1769 | ```js
|
1770 | setTimeout(fn(...args), time, ...args) -> id
|
1771 | setInterval(fn(...args), time, ...args) -> id
|
1772 | ```
|
1773 | [*CommonJS entry points:*](#commonjs)
|
1774 | ```js
|
1775 | core-js(/library)/web/timers
|
1776 | core-js(/library)/fn/set-timeout
|
1777 | core-js(/library)/fn/set-interval
|
1778 | ```
|
1779 | ```js
|
1780 | // Before:
|
1781 | setTimeout(log.bind(null, 42), 1000);
|
1782 | // After:
|
1783 | setTimeout(log, 1000, 42);
|
1784 | ```
|
1785 | #### setImmediate
|
1786 | Module [`web.immediate`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/web.immediate.js). [`setImmediate` proposal](https://developer.mozilla.org/en-US/docs/Web/API/Window.setImmediate) polyfill.
|
1787 | ```js
|
1788 | setImmediate(fn(...args), ...args) -> id
|
1789 | clearImmediate(id) -> void
|
1790 | ```
|
1791 | [*CommonJS entry points:*](#commonjs)
|
1792 | ```js
|
1793 | core-js(/library)/web/immediate
|
1794 | core-js(/library)/fn/set-immediate
|
1795 | core-js(/library)/fn/clear-immediate
|
1796 | ```
|
1797 | [*Examples*](http://goo.gl/6nXGrx):
|
1798 | ```js
|
1799 | setImmediate(function(arg1, arg2){
|
1800 | console.log(arg1, arg2); // => Message will be displayed with minimum delay
|
1801 | }, 'Message will be displayed', 'with minimum delay');
|
1802 |
|
1803 | clearImmediate(setImmediate(function(){
|
1804 | console.log('Message will not be displayed');
|
1805 | }));
|
1806 | ```
|
1807 | #### Iterable DOM collections
|
1808 | Some DOM collections should have [iterable interface](https://heycam.github.io/webidl/#idl-iterable) or should be [inherited from `Array`](https://heycam.github.io/webidl/#LegacyArrayClass). That mean they should have `keys`, `values`, `entries` and `@@iterator` methods for iteration. So add them. Module [`web.dom.iterable`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/web.dom.iterable.js):
|
1809 | ```js
|
1810 | {
|
1811 | CSSRuleList,
|
1812 | CSSStyleDeclaration,
|
1813 | CSSValueList,
|
1814 | ClientRectList,
|
1815 | DOMRectList,
|
1816 | DOMStringList,
|
1817 | DOMTokenList,
|
1818 | DataTransferItemList,
|
1819 | FileList,
|
1820 | HTMLAllCollection,
|
1821 | HTMLCollection,
|
1822 | HTMLFormElement,
|
1823 | HTMLSelectElement,
|
1824 | MediaList,
|
1825 | MimeTypeArray,
|
1826 | NamedNodeMap,
|
1827 | NodeList,
|
1828 | PaintRequestList,
|
1829 | Plugin,
|
1830 | PluginArray,
|
1831 | SVGLengthList,
|
1832 | SVGNumberList,
|
1833 | SVGPathSegList,
|
1834 | SVGPointList,
|
1835 | SVGStringList,
|
1836 | SVGTransformList,
|
1837 | SourceBufferList,
|
1838 | StyleSheetList,
|
1839 | TextTrackCueList,
|
1840 | TextTrackList,
|
1841 | TouchList
|
1842 | }
|
1843 | #@@iterator() -> iterator (values)
|
1844 |
|
1845 | {
|
1846 | DOMTokenList,
|
1847 | NodeList
|
1848 | }
|
1849 | #values() -> iterator
|
1850 | #keys() -> iterator
|
1851 | #entries() -> iterator
|
1852 | ```
|
1853 | [*CommonJS entry points:*](#commonjs)
|
1854 | ```js
|
1855 | core-js(/library)/web/dom-collections
|
1856 | core-js(/library)/fn/dom-collections/iterator
|
1857 | ```
|
1858 | [*Examples*](http://goo.gl/lfXVFl):
|
1859 | ```js
|
1860 | for(var {id} of document.querySelectorAll('*')){
|
1861 | if(id)console.log(id);
|
1862 | }
|
1863 |
|
1864 | for(var [index, {id}] of document.querySelectorAll('*').entries()){
|
1865 | if(id)console.log(index, id);
|
1866 | }
|
1867 | ```
|
1868 | ### Non-standard
|
1869 | [*CommonJS entry points:*](#commonjs)
|
1870 | ```js
|
1871 | core-js(/library)/core
|
1872 | ```
|
1873 | #### Object
|
1874 | Modules [`core.object.is-object`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/core.object.is-object.js), [`core.object.classof`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/core.object.classof.js), [`core.object.define`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/core.object.define.js), [`core.object.make`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/core.object.make.js).
|
1875 | ```js
|
1876 | Object
|
1877 | .isObject(var) -> bool
|
1878 | .classof(var) -> string
|
1879 | .define(target, mixin) -> target
|
1880 | .make(proto | null, mixin?) -> object
|
1881 | ```
|
1882 |
|
1883 | [*CommonJS entry points:*](#commonjs)
|
1884 | ```js
|
1885 | core-js(/library)/core/object
|
1886 | core-js(/library)/fn/object/is-object
|
1887 | core-js(/library)/fn/object/define
|
1888 | core-js(/library)/fn/object/make
|
1889 | ```
|
1890 | Object classify [*examples*](http://goo.gl/YZQmGo):
|
1891 | ```js
|
1892 | Object.isObject({}); // => true
|
1893 | Object.isObject(isNaN); // => true
|
1894 | Object.isObject(null); // => false
|
1895 |
|
1896 | var classof = Object.classof;
|
1897 |
|
1898 | classof(null); // => 'Null'
|
1899 | classof(undefined); // => 'Undefined'
|
1900 | classof(1); // => 'Number'
|
1901 | classof(true); // => 'Boolean'
|
1902 | classof('string'); // => 'String'
|
1903 | classof(Symbol()); // => 'Symbol'
|
1904 |
|
1905 | classof(new Number(1)); // => 'Number'
|
1906 | classof(new Boolean(true)); // => 'Boolean'
|
1907 | classof(new String('string')); // => 'String'
|
1908 |
|
1909 | var fn = function(){}
|
1910 | , list = (function(){return arguments})(1, 2, 3);
|
1911 |
|
1912 | classof({}); // => 'Object'
|
1913 | classof(fn); // => 'Function'
|
1914 | classof([]); // => 'Array'
|
1915 | classof(list); // => 'Arguments'
|
1916 | classof(/./); // => 'RegExp'
|
1917 | classof(new TypeError); // => 'Error'
|
1918 |
|
1919 | classof(new Set); // => 'Set'
|
1920 | classof(new Map); // => 'Map'
|
1921 | classof(new WeakSet); // => 'WeakSet'
|
1922 | classof(new WeakMap); // => 'WeakMap'
|
1923 | classof(new Promise(fn)); // => 'Promise'
|
1924 |
|
1925 | classof([].values()); // => 'Array Iterator'
|
1926 | classof(new Set().values()); // => 'Set Iterator'
|
1927 | classof(new Map().values()); // => 'Map Iterator'
|
1928 |
|
1929 | classof(Math); // => 'Math'
|
1930 | classof(JSON); // => 'JSON'
|
1931 |
|
1932 | function Example(){}
|
1933 | Example.prototype[Symbol.toStringTag] = 'Example';
|
1934 |
|
1935 | classof(new Example); // => 'Example'
|
1936 | ```
|
1937 | `Object.define` and `Object.make` [*examples*](http://goo.gl/rtpD5Z):
|
1938 | ```js
|
1939 | // Before:
|
1940 | Object.defineProperty(target, 'c', {
|
1941 | enumerable: true,
|
1942 | configurable: true,
|
1943 | get: function(){
|
1944 | return this.a + this.b;
|
1945 | }
|
1946 | });
|
1947 |
|
1948 | // After:
|
1949 | Object.define(target, {
|
1950 | get c(){
|
1951 | return this.a + this.b;
|
1952 | }
|
1953 | });
|
1954 |
|
1955 | // Shallow object cloning with prototype and descriptors:
|
1956 | var copy = Object.make(Object.getPrototypeOf(src), src);
|
1957 |
|
1958 | // Simple inheritance:
|
1959 | function Vector2D(x, y){
|
1960 | this.x = x;
|
1961 | this.y = y;
|
1962 | }
|
1963 | Object.define(Vector2D.prototype, {
|
1964 | get xy(){
|
1965 | return Math.hypot(this.x, this.y);
|
1966 | }
|
1967 | });
|
1968 | function Vector3D(x, y, z){
|
1969 | Vector2D.apply(this, arguments);
|
1970 | this.z = z;
|
1971 | }
|
1972 | Vector3D.prototype = Object.make(Vector2D.prototype, {
|
1973 | constructor: Vector3D,
|
1974 | get xyz(){
|
1975 | return Math.hypot(this.x, this.y, this.z);
|
1976 | }
|
1977 | });
|
1978 |
|
1979 | var vector = new Vector3D(9, 12, 20);
|
1980 | console.log(vector.xy); // => 15
|
1981 | console.log(vector.xyz); // => 25
|
1982 | vector.y++;
|
1983 | console.log(vector.xy); // => 15.811388300841896
|
1984 | console.log(vector.xyz); // => 25.495097567963924
|
1985 | ```
|
1986 | #### Dict
|
1987 | Module [`core.dict`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/core.dict.js). Based on [TC39 discuss](https://github.com/rwaldron/tc39-notes/blob/master/es6/2012-11/nov-29.md#collection-apis-review) / [strawman](http://wiki.ecmascript.org/doku.php?id=harmony:modules_standard#dictionaries).
|
1988 | ```js
|
1989 | [new] Dict(iterable (entries) | object ?) -> dict
|
1990 | .isDict(var) -> bool
|
1991 | .values(object) -> iterator
|
1992 | .keys(object) -> iterator
|
1993 | .entries(object) -> iterator (entries)
|
1994 | .has(object, key) -> bool
|
1995 | .get(object, key) -> val
|
1996 | .set(object, key, value) -> object
|
1997 | .forEach(object, fn(val, key, @), that) -> void
|
1998 | .map(object, fn(val, key, @), that) -> new @
|
1999 | .mapPairs(object, fn(val, key, @), that) -> new @
|
2000 | .filter(object, fn(val, key, @), that) -> new @
|
2001 | .some(object, fn(val, key, @), that) -> bool
|
2002 | .every(object, fn(val, key, @), that) -> bool
|
2003 | .find(object, fn(val, key, @), that) -> val
|
2004 | .findKey(object, fn(val, key, @), that) -> key
|
2005 | .keyOf(object, var) -> key
|
2006 | .includes(object, var) -> bool
|
2007 | .reduce(object, fn(memo, val, key, @), memo?) -> var
|
2008 | ```
|
2009 |
|
2010 | [*CommonJS entry points:*](#commonjs)
|
2011 | ```js
|
2012 | core-js(/library)/core/dict
|
2013 | core-js(/library)/fn/dict
|
2014 | ```
|
2015 | `Dict` create object without prototype from iterable or simple object.
|
2016 |
|
2017 | [*Examples*](http://goo.gl/pnp8Vr):
|
2018 | ```js
|
2019 | var map = new Map([['a', 1], ['b', 2], ['c', 3]]);
|
2020 |
|
2021 | Dict(); // => {__proto__: null}
|
2022 | Dict({a: 1, b: 2, c: 3}); // => {__proto__: null, a: 1, b: 2, c: 3}
|
2023 | Dict(map); // => {__proto__: null, a: 1, b: 2, c: 3}
|
2024 | Dict([1, 2, 3].entries()); // => {__proto__: null, 0: 1, 1: 2, 2: 3}
|
2025 |
|
2026 | var dict = Dict({a: 42});
|
2027 | dict instanceof Object; // => false
|
2028 | dict.a; // => 42
|
2029 | dict.toString; // => undefined
|
2030 | 'a' in dict; // => true
|
2031 | 'hasOwnProperty' in dict; // => false
|
2032 |
|
2033 | Dict.isDict({}); // => false
|
2034 | Dict.isDict(Dict()); // => true
|
2035 | ```
|
2036 | `Dict.keys`, `Dict.values` and `Dict.entries` returns iterators for objects.
|
2037 |
|
2038 | [*Examples*](http://goo.gl/xAvECH):
|
2039 | ```js
|
2040 | var dict = {a: 1, b: 2, c: 3};
|
2041 |
|
2042 | for(var key of Dict.keys(dict))console.log(key); // => 'a', 'b', 'c'
|
2043 |
|
2044 | for(var val of Dict.values(dict))console.log(val); // => 1, 2, 3
|
2045 |
|
2046 | for(var [key, val] of Dict.entries(dict)){
|
2047 | console.log(key); // => 'a', 'b', 'c'
|
2048 | console.log(val); // => 1, 2, 3
|
2049 | }
|
2050 |
|
2051 | new Map(Dict.entries(dict)); // => Map {a: 1, b: 2, c: 3}
|
2052 | ```
|
2053 | Basic dict operations for objects with prototype [*examples*](http://goo.gl/B28UnG):
|
2054 | ```js
|
2055 | 'q' in {q: 1}; // => true
|
2056 | 'toString' in {}; // => true
|
2057 |
|
2058 | Dict.has({q: 1}, 'q'); // => true
|
2059 | Dict.has({}, 'toString'); // => false
|
2060 |
|
2061 | ({q: 1})['q']; // => 1
|
2062 | ({}).toString; // => function toString(){ [native code] }
|
2063 |
|
2064 | Dict.get({q: 1}, 'q'); // => 1
|
2065 | Dict.get({}, 'toString'); // => undefined
|
2066 |
|
2067 | var O = {};
|
2068 | O['q'] = 1;
|
2069 | O['q']; // => 1
|
2070 | O['__proto__'] = {w: 2};
|
2071 | O['__proto__']; // => {w: 2}
|
2072 | O['w']; // => 2
|
2073 |
|
2074 | var O = {};
|
2075 | Dict.set(O, 'q', 1);
|
2076 | O['q']; // => 1
|
2077 | Dict.set(O, '__proto__', {w: 2});
|
2078 | O['__proto__']; // => {w: 2}
|
2079 | O['w']; // => undefined
|
2080 | ```
|
2081 | Other methods of `Dict` module are static equivalents of `Array.prototype` methods for dictionaries.
|
2082 |
|
2083 | [*Examples*](http://goo.gl/xFi1RH):
|
2084 | ```js
|
2085 | var dict = {a: 1, b: 2, c: 3};
|
2086 |
|
2087 | Dict.forEach(dict, console.log, console);
|
2088 | // => 1, 'a', {a: 1, b: 2, c: 3}
|
2089 | // => 2, 'b', {a: 1, b: 2, c: 3}
|
2090 | // => 3, 'c', {a: 1, b: 2, c: 3}
|
2091 |
|
2092 | Dict.map(dict, function(it){
|
2093 | return it * it;
|
2094 | }); // => {a: 1, b: 4, c: 9}
|
2095 |
|
2096 | Dict.mapPairs(dict, function(val, key){
|
2097 | if(key != 'b')return [key + key, val * val];
|
2098 | }); // => {aa: 1, cc: 9}
|
2099 |
|
2100 | Dict.filter(dict, function(it){
|
2101 | return it % 2;
|
2102 | }); // => {a: 1, c: 3}
|
2103 |
|
2104 | Dict.some(dict, function(it){
|
2105 | return it === 2;
|
2106 | }); // => true
|
2107 |
|
2108 | Dict.every(dict, function(it){
|
2109 | return it === 2;
|
2110 | }); // => false
|
2111 |
|
2112 | Dict.find(dict, function(it){
|
2113 | return it > 2;
|
2114 | }); // => 3
|
2115 | Dict.find(dict, function(it){
|
2116 | return it > 4;
|
2117 | }); // => undefined
|
2118 |
|
2119 | Dict.findKey(dict, function(it){
|
2120 | return it > 2;
|
2121 | }); // => 'c'
|
2122 | Dict.findKey(dict, function(it){
|
2123 | return it > 4;
|
2124 | }); // => undefined
|
2125 |
|
2126 | Dict.keyOf(dict, 2); // => 'b'
|
2127 | Dict.keyOf(dict, 4); // => undefined
|
2128 |
|
2129 | Dict.includes(dict, 2); // => true
|
2130 | Dict.includes(dict, 4); // => false
|
2131 |
|
2132 | Dict.reduce(dict, function(memo, it){
|
2133 | return memo + it;
|
2134 | }); // => 6
|
2135 | Dict.reduce(dict, function(memo, it){
|
2136 | return memo + it;
|
2137 | }, ''); // => '123'
|
2138 | ```
|
2139 | #### Partial application
|
2140 | Module [`core.function.part`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/core.function.part.js).
|
2141 | ```js
|
2142 | Function
|
2143 | #part(...args | _) -> fn(...args)
|
2144 | ```
|
2145 |
|
2146 | [*CommonJS entry points:*](#commonjs)
|
2147 | ```js
|
2148 | core-js/core/function
|
2149 | core-js(/library)/fn/function/part
|
2150 | core-js(/library)/fn/function/virtual/part
|
2151 | core-js(/library)/fn/_
|
2152 | ```
|
2153 | `Function#part` partial apply function without `this` binding. Uses global variable `_` (`core._` for builds without global namespace pollution) as placeholder and not conflict with `Underscore` / `LoDash`.
|
2154 |
|
2155 | [*Examples*](http://goo.gl/p9ZJ8K):
|
2156 | ```js
|
2157 | var fn1 = log.part(1, 2);
|
2158 | fn1(3, 4); // => 1, 2, 3, 4
|
2159 |
|
2160 | var fn2 = log.part(_, 2, _, 4);
|
2161 | fn2(1, 3); // => 1, 2, 3, 4
|
2162 |
|
2163 | var fn3 = log.part(1, _, _, 4);
|
2164 | fn3(2, 3); // => 1, 2, 3, 4
|
2165 |
|
2166 | fn2(1, 3, 5); // => 1, 2, 3, 4, 5
|
2167 | fn2(1); // => 1, 2, undefined, 4
|
2168 | ```
|
2169 | #### Number Iterator
|
2170 | Module [`core.number.iterator`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/core.number.iterator.js).
|
2171 | ```js
|
2172 | Number
|
2173 | #@@iterator() -> iterator
|
2174 | ```
|
2175 |
|
2176 | [*CommonJS entry points:*](#commonjs)
|
2177 | ```js
|
2178 | core-js(/library)/core/number
|
2179 | core-js(/library)/fn/number/iterator
|
2180 | core-js(/library)/fn/number/virtual/iterator
|
2181 | ```
|
2182 | [*Examples*](http://goo.gl/o45pCN):
|
2183 | ```js
|
2184 | for(var i of 3)console.log(i); // => 0, 1, 2
|
2185 |
|
2186 | [...10]; // => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
2187 |
|
2188 | Array.from(10, Math.random); // => [0.9817775336559862, 0.02720663254149258, ...]
|
2189 |
|
2190 | Array.from(10, function(it){
|
2191 | return this + it * it;
|
2192 | }, .42); // => [0.42, 1.42, 4.42, 9.42, 16.42, 25.42, 36.42, 49.42, 64.42, 81.42]
|
2193 | ```
|
2194 | #### Escaping strings
|
2195 | Modules [`core.regexp.escape`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/core.regexp.escape.js), [`core.string.escape-html`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/core.string.escape-html.js) and [`core.string.unescape-html`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/core.string.unescape-html.js).
|
2196 | ```js
|
2197 | RegExp
|
2198 | .escape(str) -> str
|
2199 | String
|
2200 | #escapeHTML() -> str
|
2201 | #unescapeHTML() -> str
|
2202 | ```
|
2203 | [*CommonJS entry points:*](#commonjs)
|
2204 | ```js
|
2205 | core-js(/library)/core/regexp
|
2206 | core-js(/library)/core/string
|
2207 | core-js(/library)/fn/regexp/escape
|
2208 | core-js(/library)/fn/string/escape-html
|
2209 | core-js(/library)/fn/string/unescape-html
|
2210 | core-js(/library)/fn/string/virtual/escape-html
|
2211 | core-js(/library)/fn/string/virtual/unescape-html
|
2212 | ```
|
2213 | [*Examples*](http://goo.gl/6bOvsQ):
|
2214 | ```js
|
2215 | RegExp.escape('Hello, []{}()*+?.\\^$|!'); // => 'Hello, \[\]\{\}\(\)\*\+\?\.\\\^\$\|!'
|
2216 |
|
2217 | '<script>doSomething();</script>'.escapeHTML(); // => '<script>doSomething();</script>'
|
2218 | '<script>doSomething();</script>'.unescapeHTML(); // => '<script>doSomething();</script>'
|
2219 | ```
|
2220 | #### delay
|
2221 | Module [`core.delay`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/core.delay.js). [Promise](#ecmascript-6-promise)-returning delay function, [esdiscuss](https://esdiscuss.org/topic/promise-returning-delay-function).
|
2222 | ```js
|
2223 | delay(ms) -> promise
|
2224 | ```
|
2225 | [*CommonJS entry points:*](#commonjs)
|
2226 | ```js
|
2227 | core-js(/library)/core/delay
|
2228 | core-js(/library)/fn/delay
|
2229 | ```
|
2230 | [*Examples*](http://goo.gl/lbucba):
|
2231 | ```js
|
2232 | delay(1e3).then(() => console.log('after 1 sec'));
|
2233 |
|
2234 | (async () => {
|
2235 | await delay(3e3);
|
2236 | console.log('after 3 sec');
|
2237 |
|
2238 | while(await delay(3e3))console.log('each 3 sec');
|
2239 | })();
|
2240 | ```
|
2241 | #### Helpers for iterators
|
2242 | Modules [`core.is-iterable`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/core.is-iterable.js), [`core.get-iterator`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/core.get-iterator.js), [`core.get-iterator-method`](https://github.com/zloirock/core-js/blob/v2.6.0/modules/core.get-iterator-method.js) - helpers for check iterability / get iterator in the `library` version or, for example, for `arguments` object:
|
2243 | ```js
|
2244 | core
|
2245 | .isIterable(var) -> bool
|
2246 | .getIterator(iterable) -> iterator
|
2247 | .getIteratorMethod(var) -> function | undefined
|
2248 | ```
|
2249 | [*CommonJS entry points:*](#commonjs)
|
2250 | ```js
|
2251 | core-js(/library)/fn/is-iterable
|
2252 | core-js(/library)/fn/get-iterator
|
2253 | core-js(/library)/fn/get-iterator-method
|
2254 | ```
|
2255 | [*Examples*](http://goo.gl/SXsM6D):
|
2256 | ```js
|
2257 | var list = (function(){
|
2258 | return arguments;
|
2259 | })(1, 2, 3);
|
2260 |
|
2261 | console.log(core.isIterable(list)); // true;
|
2262 |
|
2263 | var iter = core.getIterator(list);
|
2264 | console.log(iter.next().value); // 1
|
2265 | console.log(iter.next().value); // 2
|
2266 | console.log(iter.next().value); // 3
|
2267 | console.log(iter.next().value); // undefined
|
2268 |
|
2269 | core.getIterator({}); // TypeError: [object Object] is not iterable!
|
2270 |
|
2271 | var iterFn = core.getIteratorMethod(list);
|
2272 | console.log(typeof iterFn); // 'function'
|
2273 | var iter = iterFn.call(list);
|
2274 | console.log(iter.next().value); // 1
|
2275 | console.log(iter.next().value); // 2
|
2276 | console.log(iter.next().value); // 3
|
2277 | console.log(iter.next().value); // undefined
|
2278 |
|
2279 | console.log(core.getIteratorMethod({})); // undefined
|
2280 | ```
|
2281 |
|
2282 | ## Missing polyfills
|
2283 | - ES5 `JSON` is missing now only in IE7- and never will it be added to `core-js`, if you need it in these old browsers, many implementations are available, for example, [json3](https://github.com/bestiejs/json3).
|
2284 | - ES6 `String#normalize` is not a very useful feature, but this polyfill will be very large. If you need it, you can use [unorm](https://github.com/walling/unorm/).
|
2285 | - ES6 `Proxy` can't be polyfilled, but for Node.js / Chromium with additional flags you can try [harmony-reflect](https://github.com/tvcutsem/harmony-reflect) for adapt old style `Proxy` API to final ES6 version.
|
2286 | - ES6 logic for `@@isConcatSpreadable` and `@@species` (in most places) can be polyfilled without problems, but it will cause a serious slowdown in popular cases in some engines. It will be polyfilled when it will be implemented in modern engines.
|
2287 | - ES7 `SIMD`. `core-js` doesn't add polyfill of this feature because of large size and some other reasons. You can use [this polyfill](https://github.com/tc39/ecmascript_simd/blob/master/src/ecmascript_simd.js).
|
2288 | - `window.fetch` is not a cross-platform feature, in some environments it makes no sense. For this reason, I don't think it should be in `core-js`. Looking at a large number of requests it *may be* added in the future. Now you can use, for example, [this polyfill](https://github.com/github/fetch).
|
2289 | - ECMA-402 `Intl` is missed because of size. You can use [this polyfill](https://github.com/andyearnshaw/Intl.js/).
|