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