首页
/ ES6 Shim 技术文档

ES6 Shim 技术文档

2024-12-24 01:02: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
    
登录后查看全文

项目优选

收起
kernelkernel
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
511
551
docsdocs
暂无描述
Markdown
852
5.69 K
ops-transformerops-transformer
本项目是CANN提供的transformer类大模型算子库,实现网络在NPU上加速计算。
C++
1.05 K
2.49 K
kernelkernel
deepin linux kernel
C
33
16
pytorchpytorch
作为 Ascend for PyTorch 社区的核心组件,TorchNPU 是昇腾专为 PyTorch 打造的深度学习适配插件,使 PyTorch 框架能够直接调用昇腾 NPU,为开发者提供昇腾 AI 处理器的超强算力。
Python
839
1.28 K
ops-nnops-nn
本项目是CANN提供的神经网络类计算算子库,实现网络在NPU上加速计算。
C++
847
1.7 K
ops-mathops-math
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
1.25 K
1.38 K
jiuwenswarmjiuwenswarm
JiuwenSwarm 是一款基于openJiuwen开发的智能AI Agent,它能够将大语言模型的强大能力,通过你日常使用的各类通讯应用,直接延伸至你的指尖。
Python
3.17 K
857
AscendNPU-IRAscendNPU-IR
AscendNPU-IR是基于MLIR(Multi-Level Intermediate Representation)构建的,面向昇腾亲和算子编译时使用的中间表示,提供昇腾完备表达能力,通过编译优化提升昇腾AI处理器计算效率,支持通过生态框架使能昇腾AI处理器与深度调优
C++
503
346
cann-learning-hubcann-learning-hub
CANN 学习中心仓,支持在线互动运行、边学边练,提供教程、示例与优化方案,一站式助力昇腾开发者快速上手。
Jupyter Notebook
787
415