首页
/ ES6 Shim 技术文档

ES6 Shim 技术文档

2024-12-24 22:36:22作者:咎竹峻Karen

安装指南

浏览器环境

  1. 在您的脚本之前引入 es6-shim
  2. 如果您的浏览器不支持 ECMAScript 5,请特别引入 es5-shimes5-shim 应该在 es6-shim 之前加载。

Node.js/io.js 或 npm 管理的工作流

推荐使用以下命令安装:

npm install es6-shim

其他安装方式

  • 使用 component(1)
    component install paulmillr/es6-shim
    
  • 使用 Bower
    bower install es6-shim
    

项目使用说明

ES6 Shim 提供了兼容性垫片,使得旧版 JavaScript 引擎尽可能接近 ECMAScript 6 (Harmony) 的行为。它涵盖了 MapSetPromise 等 ES6 特性,并提供了许多字符串、数组、对象、数学函数等的增强功能。

示例代码

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));

项目API使用文档

安全垫片

  • Map:需要 ES5 属性描述符支持。
  • Set:需要 ES5 属性描述符支持。
  • Promise:提供 Promise 支持。
  • String
    • fromCodePoint()
    • raw()
  • String.prototype
    • codePointAt()
    • endsWith()
    • includes()
    • repeat()
    • startsWith()
  • RegExp
    • new RegExp:当给定 RegExp 作为模式时,不再抛出错误。
    • RegExp.prototype
      • flags
      • [Symbol.match]
      • [Symbol.replace]
      • [Symbol.search]
      • [Symbol.split]
      • toString
  • Number
    • 二进制和八进制字面量
    • 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()
  • Object
    • assign()
    • is()
    • keys()
    • setPrototypeOf()
  • Function.prototype
    • name
  • Math
    • acosh()
    • asinh()
    • atanh()
    • cbrt()
    • clz32()
    • cosh()
    • expm1()
    • fround()
    • hypot()
    • imul()
    • log10()
    • log1p()
    • log2()
    • sign()
    • sinh()
    • tanh()
    • trunc()
  • Reflect
    • apply()
    • construct()
    • defineProperty()
    • deleteProperty()
    • get()
    • getOwnPropertyDescriptor()
    • getPrototypeOf()
    • has()
    • isExtensible()
    • ownKeys()
    • preventExtensions()
    • set()
    • setPrototypeOf()

子类化

MapSetPromise 实现是可子类化的。您可以使用以下模式在 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 }
});

项目安装方式

浏览器环境

  • 直接引入 es6-shim 脚本。
  • 如果需要,引入 es5-shim

Node.js/io.js 或 npm 管理的工作流

npm install es6-shim

其他安装方式

  • 使用 component(1)
    component install paulmillr/es6-shim
    
  • 使用 Bower
    bower install es6-shim
    
登录后查看全文
热门项目推荐
相关项目推荐