JavaScript 的模块化标准库。包括截至 2024 年的 ECMAScript 填充:承诺、符号、集合、迭代器、类型化数组、许多其他功能、ECMAScript 提案、一些跨平台 WHATWG / W3C 功能和
URL
等提案。您可以仅加载所需的功能或在没有全局名称空间污染的情况下使用它。
如果您正在寻找已过时的core-js@2
的文档,请检查此分支。
core-js
没有公司支持,所以这个项目的未来取决于你。如果您对core-js
感兴趣,请成为赞助商或支持者: Open Collective 、 Patreon 、 Boosty 、 Bitcoin (bc1qlea7544qtsmj2rayg0lthvza9fau63ux0fstcz) 、 Alipay 。
使用示例:
import 'core-js/actual' ;
Promise . resolve ( 42 ) . then ( it => console . log ( it ) ) ; // => 42
Array . from ( new Set ( [ 1 , 2 , 3 ] ) . union ( new Set ( [ 3 , 4 , 5 ] ) ) ) ; // => [1, 2, 3, 4, 5]
[ 1 , 2 ] . flatMap ( it => [ it , it ] ) ; // => [1, 1, 2, 2]
( function * ( i ) { while ( true ) yield i ++ ; } ) ( 1 )
. drop ( 1 ) . take ( 5 )
. filter ( it => it % 2 )
. map ( it => it ** 2 )
. toArray ( ) ; // => [9, 25]
structuredClone ( new Set ( [ 1 , 2 , 3 ] ) ) ; // => new Set([1, 2, 3])
您可以仅加载所需的功能:
import 'core-js/actual/promise' ;
import 'core-js/actual/set' ;
import 'core-js/actual/iterator' ;
import 'core-js/actual/array/from' ;
import 'core-js/actual/array/flat-map' ;
import 'core-js/actual/structured-clone' ;
Promise . resolve ( 42 ) . then ( it => console . log ( it ) ) ; // => 42
Array . from ( new Set ( [ 1 , 2 , 3 ] ) . union ( new Set ( [ 3 , 4 , 5 ] ) ) ) ; // => [1, 2, 3, 4, 5]
[ 1 , 2 ] . flatMap ( it => [ it , it ] ) ; // => [1, 1, 2, 2]
( function * ( i ) { while ( true ) yield i ++ ; } ) ( 1 )
. drop ( 1 ) . take ( 5 )
. filter ( it => it % 2 )
. map ( it => it ** 2 )
. toArray ( ) ; // => [9, 25]
structuredClone ( new Set ( [ 1 , 2 , 3 ] ) ) ; // => new Set([1, 2, 3])
或者在没有全局名称空间污染的情况下使用它:
import Promise from 'core-js-pure/actual/promise' ;
import Set from 'core-js-pure/actual/set' ;
import Iterator from 'core-js-pure/actual/iterator' ;
import from from 'core-js-pure/actual/array/from' ;
import flatMap from 'core-js-pure/actual/array/flat-map' ;
import structuredClone from 'core-js-pure/actual/structured-clone' ;
Promise . resolve ( 42 ) . then ( it => console . log ( it ) ) ; // => 42
from ( new Set ( [ 1 , 2 , 3 ] ) . union ( new Set ( [ 3 , 4 , 5 ] ) ) ) ; // => [1, 2, 3, 4, 5]
flatMap ( [ 1 , 2 ] , it => [ it , it ] ) ; // => [1, 1, 2, 2]
Iterator . from ( function * ( i ) { while ( true ) yield i ++ ; } ( 1 ) )
. drop ( 1 ) . take ( 5 )
. filter ( it => it % 2 )
. map ( it => it ** 2 )
. toArray ( ) ; // => [9, 25]
structuredClone ( new Set ( [ 1 , 2 , 3 ] ) ) ; // => new Set([1, 2, 3])
postinstall
消息@babel/polyfill
@babel/preset-env
@babel/runtime
globalThis
Array.prototype.includes
Array.prototype.flat
/ Array.prototype.flatMap
Array
从最后查找Array
Array
分组ArrayBuffer.prototype.transfer
和朋友Iterator
助手Object.values
/ Object.entries
Object.fromEntries
Object.getOwnPropertyDescriptors
Object.prototype.hasOwnProperty
String
填充String.prototype.matchAll
String.prototype.replaceAll
String.prototype.trimStart
/ String.prototype.trimEnd
RegExp
s
( dotAll
) 标志RegExp
命名捕获组Promise.allSettled
Promise.any
Promise.prototype.finally
Promise.try
Promise.withResolvers
Symbol.asyncIterator
用于异步迭代Symbol.prototype.description
JSON.stringify
Set
方法Array.fromAsync
JSON.parse
源文本访问Float16
方法Uint8Array
与 Base64 和十六进制之间的转换RegExp
表达式转义Math.sumPrecise
Symbol.metadata
Iterator
排序AsyncIterator
助手Iterator.range
Map
更新插入Array.isTemplateObject
String.dedent
Symbol
谓词Symbol.customMatcher
Observable
.of
和.from
方法compositeKey
和compositeSymbol
Array
过滤Array
去重DataView
获取/设置Uint8Clamped
方法Number.fromString
String.cooked
String.prototype.codePoints
Symbol.customMatcher
用于模式匹配Function.prototype.demethodize
Function.{ isCallable, isConstructor }
Reflect
元数据self
structuredClone
setTimeout
和setInterval
setImmediate
queueMicrotask
URL
和URLSearchParams
DOMException
// global version
npm install --save [email protected]
// version without global namespace pollution
npm install --save [email protected]
// bundled global version
npm install --save [email protected]
或者您可以使用 CDN 中的core-js
。
postinstall
消息⬆ core-js
项目需要您的帮助,因此该包在安装后会显示一条有关它的消息。如果它给您带来问题,您可以禁用它:
ADBLOCK=true npm install
// or
DISABLE_OPENCOLLECTIVE=true npm install
// or
npm install --loglevel silent
您可以导入仅需要的填充,如README.md
顶部的示例所示。所有填充方法/构造函数和命名空间的可用 CommonJS 入口点。仅举一些例子:
// polyfill all `core-js` features, including early-stage proposals:
import "core-js" ;
// or:
import "core-js/full" ;
// polyfill all actual features - stable ES, web standards and stage 3 ES proposals:
import "core-js/actual" ;
// polyfill only stable features - ES and web standards:
import "core-js/stable" ;
// polyfill only stable ES features:
import "core-js/es" ;
// if you want to polyfill `Set`:
// all `Set`-related features, with early-stage ES proposals:
import "core-js/full/set" ;
// stable required for `Set` ES features, features from web standards and stage 3 ES proposals:
import "core-js/actual/set" ;
// stable required for `Set` ES features and features from web standards
// (DOM collections iterator in this case):
import "core-js/stable/set" ;
// only stable ES features required for `Set`:
import "core-js/es/set" ;
// the same without global namespace pollution:
import Set from "core-js-pure/full/set" ;
import Set from "core-js-pure/actual/set" ;
import Set from "core-js-pure/stable/set" ;
import Set from "core-js-pure/es/set" ;
// if you want to polyfill just the required methods:
import "core-js/full/set/intersection" ;
import "core-js/actual/array/find-last" ;
import "core-js/stable/queue-microtask" ;
import "core-js/es/array/from" ;
// polyfill iterator helpers proposal:
import "core-js/proposals/iterator-helpers" ;
// polyfill all stage 2+ proposals:
import "core-js/stage/2" ;
提示
建议使用/actual/
命名空间,因为它包含所有实际的 JavaScript 功能,并且不包含主要用于实验的不稳定的早期提案。
警告
modules
路径是内部 API,不会注入所有必需的依赖项,并且可以在次要版本或补丁版本中更改。仅将其用于自定义构建和/或如果您知道自己在做什么。core-js
,建议在应用程序入口点顶部加载所有core-js
模块,否则可能会发生冲突。Symbol.iterator
,与Array.from
、 URLSearchParams
和/或core-js
中的其他内容冲突,请参阅相关问题。core-js
中的每个冲突条目来解决。core-js
非常模块化,使用了很多非常小的模块,因为在浏览器中使用时会捆绑core-js
而不是每个文件的使用加载器,否则,您将有数百个请求。 在pure
版本中,我们不能污染本机构造函数的原型。因此,原型方法转换为静态方法,如上面的示例所示。但对于转译器,我们还可以使用一种技巧——绑定运算符和虚拟方法。特别是,可用的/virtual/
入口点。例子:
import fill from 'core-js-pure/actual/array/virtual/fill' ;
import findIndex from 'core-js-pure/actual/array/virtual/find-index' ;
Array ( 10 ) :: fill ( 0 ) . map ( ( a , b ) => b * b ) :: findIndex ( it => it && ! ( it % 8 ) ) ; // => 4
警告
绑定运算符是早期的 ECMAScript 提案,使用此语法可能很危险。
core-js
与babel
集成,是与 polyfilling 相关的babel
功能的基础:
@babel/polyfill
⬆ @babel/polyfill
只是稳定的core-js
功能以及生成器和异步函数的regenerator-runtime
的导入,因此如果您加载@babel/polyfill
- 您将加载core-js
的全局版本,而无需 ES 建议。
现在它已被弃用,取而代之的是单独包含core-js
和regenerator-runtime
所需的部分,并且为了防止重大更改,保留在core-js@2
上。
作为@babel/polyfill
的完全等价物,您可以使用它:
import 'core-js/stable' ;
import 'regenerator-runtime/runtime' ;
@babel/preset-env
⬆ @babel/preset-env
有useBuiltIns
选项,它优化了core-js
的全局版本的使用。使用useBuiltIns
选项,您还应该将corejs
选项设置为core-js
的使用版本,例如corejs: '3.39'
。
重要的
建议指定使用的次要core-js
版本,例如corejs: '3.39'
,而不是corejs: 3
,因为corejs: 3
不会注入在次要core-js
版本中添加的模块。
useBuiltIns: 'entry'
替换core-js
的导入,仅导入目标环境所需的模块。所以,举例来说, import 'core-js/stable' ;
chrome 71
目标将被替换为:
import 'core-js/modules/es.array.unscopables.flat' ;
import 'core-js/modules/es.array.unscopables.flat-map' ;
import 'core-js/modules/es.object.from-entries' ;
import 'core-js/modules/web.immediate' ;
它适用于core-js
全局版本及其组合的所有入口点,例如
import 'core-js/es' ;
import 'core-js/proposals/set-methods' ;
import 'core-js/full/set/map' ;
使用chrome 71
目标,您将得到以下结果:
import 'core-js/modules/es.array.unscopables.flat' ;
import 'core-js/modules/es.array.unscopables.flat-map' ;
import 'core-js/modules/es.object.from-entries' ;
import 'core-js/modules/esnext.set.difference' ;
import 'core-js/modules/esnext.set.intersection' ;
import 'core-js/modules/esnext.set.is-disjoint-from' ;
import 'core-js/modules/esnext.set.is-subset-of' ;
import 'core-js/modules/esnext.set.is-superset-of' ;
import 'core-js/modules/esnext.set.map' ;
import 'core-js/modules/esnext.set.symmetric-difference' ;
import 'core-js/modules/esnext.set.union' ;
useBuiltIns: 'usage'
添加到每个文件的顶部导入该文件中使用的且目标环境不支持的功能的polyfills,因此对于: // first file:
let set = new Set ( [ 1 , 2 , 3 ] ) ;
// second file:
let array = Array . of ( 1 , 2 , 3 ) ;
如果目标包含像IE 11
这样的旧环境,我们将得到如下内容:
// first file:
import 'core-js/modules/es.array.iterator' ;
import 'core-js/modules/es.object.to-string' ;
import 'core-js/modules/es.set' ;
var set = new Set ( [ 1 , 2 , 3 ] ) ;
// second file:
import 'core-js/modules/es.array.of' ;
var array = Array . of ( 1 , 2 , 3 ) ;
默认情况下,带有useBuiltIns: 'usage'
选项的@babel/preset-env
仅填充稳定功能,但您可以通过proposals
选项启用提案的填充,如corejs: { version: '3.39', proposals: true }
。
重要的
在useBuiltIns: 'usage'
的情况下,您不应该自己添加core-js
导入,它们会自动添加。
@babel/runtime
⬆ @babel/runtime
with corejs: 3
选项简化了core-js-pure
使用。它会自动替换 JS 标准库中现代功能的使用,以从core-js
版本导入,而不会造成全局命名空间污染,因此改为:
import from from 'core-js-pure/stable/array/from' ;
import flat from 'core-js-pure/stable/array/flat' ;
import Set from 'core-js-pure/stable/set' ;
import Promise from 'core-js-pure/stable/promise' ;
from ( new Set ( [ 1 , 2 , 3 , 2 , 1 ] ) ) ;
flat ( [ 1 , [ 2 , 3 ] , [ 4 , [ 5 ] ] ] , 2 ) ;
Promise . resolve ( 32 ) . then ( x => console . log ( x ) ) ;
你可以只写:
Array . from ( new Set ( [ 1 , 2 , 3 , 2 , 1 ] ) ) ;
[ 1 , [ 2 , 3 ] , [ 4 , [ 5 ] ] ] . flat ( 2 ) ;
Promise . resolve ( 32 ) . then ( x => console . log ( x ) ) ;
默认情况下, @babel/runtime
仅填充稳定功能,但与@babel/preset-env
一样,您可以通过proposals
选项启用提案的填充,如corejs: { version: 3, proposals: true }
。
警告
如果同时使用@babel/preset-env
和@babel/runtime
,请仅在一处使用corejs
选项,因为它是重复的功能并且会导致冲突。
快速 JavaScript 转译器swc
包含与core-js
集成,可优化与core-js
的全球版本的工作。与@babel/preset-env
一样,它有 2 种模式: usage
和entry
,但usage
模式仍然不如babel
中那么好用。 .swcrc
中的配置示例:
{
"env" : {
"targets" : " > 0.25%, not dead " ,
"mode" : " entry " ,
"coreJs" : " 3.39 "
}
}
默认情况下, core-js
仅在需要时才设置 polyfill。这意味着core-js
检查某个功能是否可用并正常工作,如果没有问题, core-js
将使用本机实现。
但有时core-js
功能检测对于您的情况可能过于严格。例如, Promise
构造函数需要未处理的拒绝跟踪和@@species
的支持。
有时我们可能会遇到相反的问题 - 一个故意损坏的环境,其问题未被core-js
功能检测覆盖。
对于这些情况,我们可以为某些 polyfill 重新定义此行为:
const configurator = require ( 'core-js/configurator' ) ;
configurator ( {
useNative : [ 'Promise' ] , // polyfills will be used only if natives are completely unavailable
usePolyfill : [ 'Array.from' , 'String.prototype.padEnd' ] , // polyfills will be used anyway
useFeatureDetection : [ 'Map' , 'Set' ] , // default behavior
} ) ;
require ( 'core-js/actual' ) ;
它不适用于某些功能。另外,如果更改默认行为,即使是core-js
内部也可能无法正常工作。
在某些情况下,排除某些core-js
功能或为目标引擎生成一个 polyfill 可能很有用。您可以使用core-js-builder
包来实现这一点。
core-js
尝试通过 ES3 支持来支持所有可能的 JS 引擎和环境。有些功能有较高的较低标准 - 例如,某些访问器只能在 ES5 中正常工作,promise 需要一种设置微任务或任务的方法,等等。
然而,我不可能在任何地方测试core-js
- 例如,在 IE7 中测试 - 和其他一些古老的东西被停止了。您可以通过下面的链接在兼容性表中查看绝对支持的引擎列表。如果您对任何引擎的支持有任何问题或疑问,请写下来。
core-js
项目提供(作为core-js-compat
包)有关core-js
模块、入口点和使用它的工具的必要性的所有必需数据 - 它对于与babel
或swc
等工具集成非常有用。如果您想提供帮助,您可以查看CONTRIBUTING.md
的相关部分。兼容性数据和浏览器测试运行程序的可视化可在此处使用,示例:
CommonJS 入口点:
core-js(-pure)
CommonJS 入口点:
core-js(-pure)/es
模块es.object.assign
、 es.object.create
、 es.object.define-getter
、 es.object.define-property
、 es.object.define-properties
、 es.object.define-setter
、 es.object.entries
、 es.object.freeze
、 es.object.from-entries
、 es.object.get-own-property-descriptor
、 es.object.get-own-property-descriptors
、 es.object.get-own-property-names
、 es.object.get-prototype-of
、 es.object.group-by
、 es.object.has-own
、 es.object.is
、 es.object.is-extensible
、 es.object.is-frozen
、 es.object.is-sealed
、 es.object.keys
、 es.object.lookup-setter
、 es.object.lookup-getter
、 es.object.prevent-extensions
、 es.object.proto
、 es.object.to-string
、 es.object.seal
、 es.object.set-prototype-of
, es.object.values
。
class Object {
toString ( ) : string ; // ES2015+ fix: @@toStringTag support
__defineGetter__ ( property : PropertyKey , getter : Function ) : void ;
__defineSetter__ ( property : PropertyKey , setter : Function ) : void ;
__lookupGetter__ ( property : PropertyKey ) : Function | void ;
__lookupSetter__ ( property : PropertyKey ) : Function | void ;
__proto__ : Object | null ; // required a way setting of prototype - will not in IE10-, it's for modern engines like Deno
static assign ( target : Object , ... sources : Array < Object > ) : Object ;
static create ( prototype : Object | null , properties ?: { [ property : PropertyKey ] : PropertyDescriptor } ) : Object ;
static defineProperties ( object : Object , properties : { [ property : PropertyKey ] : PropertyDescriptor } ) ) :