es6 shim
v0.35.6
提供兼容性墊片,以使傳統的JavaScript引擎與Ecmascript 6(Harmony)盡可能緊密地表現。
HTML版本的最終ecmascript 6規格
如果您想在瀏覽器中使用它:
es6-shim
即可。es5-shim
糾正損壞的實現,因此強烈建議始終包含它。另外,在es6-shim
之前應加載es5-shim
。對於node.js
, io.js
或任何npm
管理的工作流(這是推薦的方法):
npm install es6-shim
替代方法:
component install paulmillr/es6-shim
如果您使用的是組件(1)。bower install es6-shim
。在瀏覽器和節點中,您可能還需要包括unorm
;有關詳細信息,請參見String.prototype.normalize
部分。
Map
(需要ES5屬性描述符支持)(也可以使用獨立的墊片)Set
(需要ES5屬性描述符支持)(也可以使用獨立的墊片)Promise
String
:fromCodePoint()
(也可以使用獨立的墊片)raw()
(也可以使用Stanadlone墊片)String.prototype
:codePointAt()
(也可以使用獨立的墊片)endsWith()
(也可以使用獨立的墊片)includes()
(也可以使用獨立的墊片)repeat()
(也可以使用獨立的墊片)startsWith()
(也可以使用獨立的墊片)RegExp
:new RegExp
將不再在給出“標誌”字符串參數時投擲。 (需要ES5)RegExp.prototype
:flags
(需要ES5)(也可以使用獨立的墊片)[Symbol.match]
(需要本機Symbol
S)[Symbol.replace]
(需要本機Symbol
S)[Symbol.search]
(需要本機Symbol
S)[Symbol.split]
(需要本地Symbol
S)toString
Number
:Number('0b1')
和Number('0o7')
EPSILON
(也可以使用獨立的墊片)MAX_SAFE_INTEGER
(也可以使用獨立的墊片)MIN_SAFE_INTEGER
(也可以使用獨立的墊片)isNaN()
(也可以使用獨立的墊片)isInteger()
(也可以使用獨立的墊片)isSafeInteger()
(也可以使用獨立的墊片)isFinite()
(也可以使用獨立的墊片)parseInt()
(也可以使用獨立的墊片)parseFloat()
Array
:from()
(也可以使用獨立的墊片)of()
(也有獨立的墊片)Array.prototype
:copyWithin()
(也可以使用獨立的墊片)entries()
(也可以使用獨立的墊片)fill()
find()
(也可以使用獨立的墊片)findIndex()
(也可以使用獨立的墊片)keys()
(也可以使用獨立的墊片)values()
(也可以使用獨立的墊片)indexOf()
(es6 errata)(也可以使用獨立的墊片)Object
:assign()
(也可以使用獨立的墊片)is()
(也可以使用獨立的墊片)keys()
(在ES5中,但不再對ES6中的非對象的非null/undefined值扔)(也可以使用獨立的墊片setPrototypeOf()
(ie> = 11)Function.prototype
:name
(ES6-SHAM,覆蓋IE 9-11)(也可以使用獨立的墊片Math
:acosh()
(也可以使用獨立的墊片)asinh()
atanh()
(也可以使用獨立的墊片)cbrt()
(也可以使用獨立的墊片)clz32()
(也可以使用獨立的墊片)cosh()
expm1()
fround()
(也可以使用獨立的墊片)hypot()
imul()
(也可以使用獨立的墊片)log10()
(也可以使用獨立的墊片)log1p()
(也可以使用獨立的墊片)log2()
sign()
(也可以使用獨立的墊片)sinh()
tanh()
trunc()
數學功能的精度為1E-11。
Reflect
apply()
(也可以使用獨立的墊片)construct()
defineProperty()
deleteProperty()
get()
getOwnPropertyDescriptor()
getPrototypeOf()
(也可以使用獨立的墊片)has()
isExtensible()
ownKeys()
(也可以使用獨立的墊片)preventExtensions()
set()
setPrototypeOf()
Symbol
(僅當它已經存在時)
match
(以及對應的String#match
, String#startsWith
, String#endsWith
, String#includes
, RegExp
支持)replace
(和對應的String#replace
支持)search
(和對應的String#search
支持)split
(和對應的String#split
支持)僅當引擎已經具有Symbol
支持時,才能提供眾所周知的符號。
String.prototype
附件B HTML方法(也可以使用獨立的墊片)anchor()
big()
blink()
bold()
fixed()
fontcolor()
fontsize()
italics()
link()
small()
strike()
sub()
sup()
這些方法是“附件B”的一部分,這意味著儘管它們是Defatso標準,但您不應該使用它們。儘管如此, es6-shim
提供了它們,並在瀏覽器中將其行為歸一化。
Map
, Set
和Promise
實現是可劃分的。您應該使用以下模式在ES5中創建一個子類,該子類將繼續在ES6中工作:
require ( 'es6-shim' ) ;
function MyPromise ( exec ) {
var promise = new Promise ( exec ) ;
Object . setPrototypeOf ( promise , MyPromise . prototype ) ;
// ...
return promise ;
}
Object . setPrototypeOf ( MyPromise , Promise ) ;
MyPromise . prototype = Object . create ( Promise . prototype , {
constructor : { value : MyPromise }
} ) ;
包括適合字符串的墊片String.prototype.normalize
將該庫的大小增加4個以上。因此,如果您需要String.prototype.normalize
我們建議您將unorm
軟件包與es6-shim
一起安裝。有關更多討論,請參見#134。
不可能在純JavaScript中實現弱圖。 ES6-Collections實現並不能持有強烈的值,這對於集合至關重要。 es6-shim
決定不包括不正確的墊片。
WeakMap
具有非常不尋常的用例,因此您可能根本不需要它(而是使用簡單的Map
)。
require ( 'es6-shim' ) ;
var assert = require ( 'assert' ) ;
assert . equal ( true , 'abc' . startsWith ( 'a' ) ) ;
assert . equal ( false , 'abc' . endsWith ( 'a' ) ) ;
assert . equal ( true , 'john alice' . includes ( 'john' ) ) ;
assert . equal ( '123' . repeat ( 2 ) , '123123' ) ;
assert . equal ( false , NaN === NaN ) ;
assert . equal ( true , Object . is ( NaN , NaN ) ) ;
assert . equal ( true , - 0 === 0 ) ;
assert . equal ( false , Object . is ( - 0 , 0 ) ) ;
var result = Object . assign ( { a : 1 } , { b : 2 } ) ;
assert . deepEqual ( result , { a : 1 , b : 2 } ) ;
assert . equal ( true , isNaN ( 'a' ) ) ;
assert . equal ( false , Number . isNaN ( 'a' ) ) ;
assert . equal ( true , Number . isNaN ( NaN ) ) ;
assert . equal ( true , isFinite ( '123' ) ) ;
assert . equal ( false , Number . isFinite ( '123' ) ) ;
assert . equal ( false , Number . isFinite ( Infinity ) ) ;
// Tests if value is a number, finite,
// >= -9007199254740992 && <= 9007199254740992 and floor(value) === value
assert . equal ( false , Number . isInteger ( 2.4 ) ) ;
assert . equal ( 1 , Math . sign ( 400 ) ) ;
assert . equal ( 0 , Math . sign ( 0 ) ) ;
assert . equal ( - 1 , Math . sign ( - 400 ) ) ;
var found = [ 5 , 10 , 15 , 10 ] . find ( function ( item ) { return item / 2 === 5 ; } ) ;
assert . equal ( 10 , found ) ;
var foundIndex = [ 5 , 10 , 15 , 10 ] . findIndex ( function ( item ) { return item / 2 === 5 ; } ) ;
assert . equal ( 1 , foundIndex ) ;
// Replacement for `{}` key-value storage.
// Keys can be anything.
var map = new Map ( [ [ 'Bob' , 42 ] , [ 'Foo' , 'bar' ] ] ) ;
map . set ( 'John' , 25 ) ;
map . set ( 'Alice' , 400 ) ;
map . set ( [ 'meh' ] , 555 ) ;
assert . equal ( undefined , map . get ( [ 'meh' ] ) ) ; // undefined because you need to use exactly the same object.
map . delete ( 'Alice' ) ;
map . keys ( ) ;
map . values ( ) ;
assert . equal ( 4 , map . size ) ;
// Useful for storing unique items.
var set = new Set ( [ 0 , 1 ] ) ;
set . add ( 2 ) ;
set . add ( 5 ) ;
assert . equal ( true , set . has ( 0 ) ) ;
assert . equal ( true , set . has ( 1 ) ) ;
assert . equal ( true , set . has ( 2 ) ) ;
assert . equal ( false , set . has ( 4 ) ) ;
assert . equal ( true , set . has ( 5 ) ) ;
set . delete ( 5 ) ;
assert . equal ( false , set . has ( 5 ) ) ;
// Promises, see
// http://www.slideshare.net/domenicdenicola/callbacks-promises-and-coroutines-oh-my-the-evolution-of-asynchronicity-in-javascript
// https://github.com/petkaantonov/bluebird/#what-are-promises-and-why-should-i-use-them
Promise . resolve ( 5 ) . then ( function ( value ) {
assert . equal ( value , 5 ) ;
if ( value ) throw new Error ( 'whoops!' ) ;
// do some stuff
return anotherPromise ( ) ;
} ) . catch ( function ( e ) {
assert . equal ( e . message , 'whoops!' ) ;
assert . equal ( true , e instanceof Error ) ;
// any errors thrown asynchronously end up here
} ) ;
Object.setPrototypeOf
/ Reflect.setPrototypeOf
Object.create(null)
,例如,帶有null
對象,因為其[[Prototype]]
)無法更改其[[Prototype]]
Object.setPrototypeOf
Symbol
sSymbol.for
進行全局Symbol
註冊表創建它們。這並不違反規格,但這確實意味著Symbol.for('Symbol.search') === Symbol.search
是true
,默認情況下它不會在新鮮的綜合領域中。 該項目最初是基於Axel Rauschmayer的ES6-Shim。