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 } ) ) :