V8最新支持的一些语法简介

V8在v7.0版本后,支持了一些新的语法,如需验证以下语法,需使用对应的支持版本

chrome 最新版本支持到v7.1, chrome金丝雀版本支持v7.2 , Node.js 11.0.0版本后支持到v7.0

v7.0 (2018.10.15)

新增:Symbol.description
复制代码

v7.1 (2018.10.31)

相对时间函数

const rtf = new Intl.RelativeTimeFormat('en');

rtf.format(3.14, 'second');
// → 'in 3.14 seconds'

rtf.format(-15, 'minute');
// → '15 minutes ago'

// 本地化相对时间
const rtf = new Intl.RelativeTimeFormat('zh', { numeric: 'auto' });

rtf.format(-1, 'day');
// → '昨天'

rtf.format(0, 'day');
// → '今天'

rtf.format(1, 'day');
// → '明天'

rtf.format(-1, 'week');
// → '上周'

rtf.format(0, 'week');
// → '本周'

rtf.format(1, 'week');
// → '下周'
复制代码

v7.2版本 (2018.12.18)

新增对公共 class 字段(public class fields) 的支持,例如下面的旧写法:

class Animal {
  constructor(name) {
    this.name = name;
  }
}
class Cat extends Animal {
  constructor(name) {
    super(name);
    this.likesBaths = false;
  }
  meow() {
    console.log('Meow!');
  }
}
复制代码

现在可以写成:

class Animal {
  constructor(name) {
    this.name = name;
  }
}
class Cat extends Animal {
  likesBaths = false;
  meow() {
    console.log('Meow!');
  }
}
复制代码

V8 计划支持 private class fields

class IncreasingCounter {
  #count = 0; // 私有属性
  get value() {
    console.log('Getting the current value!');
    return this.#count;
  }
  increment() {
    this.#count++;
  }
}

const counter = new IncreasingCounter();
counter.#count;
// → SyntaxError
counter.#count = 42;
// → SyntaxError

复制代码

JSON.stringify 支持 Unicode 输出

// 老版本会输出无法解析的字符
// 新支持后,输出 '\uD800' 字符串
JSON.stringify('\uD800');
复制代码

数据格式化

const lf = new Intl.ListFormat('en');
lf.format(['Frank']);
// → 'Frank'
lf.format(['Frank', 'Christine']);
// → 'Frank and Christine'
lf.format(['Frank', 'Christine', 'Flora']);
// → 'Frank, Christine, and Flora'
lf.format(['Frank', 'Christine', 'Flora', 'Harrison']);
// → 'Frank, Christine, Flora, and Harrison'

const lf = new Intl.ListFormat('en', { type: 'disjunction' });
lf.format(['Frank']);
// → 'Frank'
lf.format(['Frank', 'Christine']);
// → 'Frank or Christine'
lf.format(['Frank', 'Christine', 'Flora']);
// → 'Frank, Christine, or Flora'
lf.format(['Frank', 'Christine', 'Flora', 'Harrison']);
// → 'Frank, Christine, Flora, or Harrison'

const lf = new Intl.ListFormat('zh');
lf.format(['永鋒']);
// → '永鋒'
lf.format(['永鋒', '新宇']);
// → '永鋒和新宇'
lf.format(['永鋒', '新宇', '芳遠']);
// → '永鋒、新宇和芳遠'
lf.format(['永鋒', '新宇', '芳遠', '澤遠']);
// → '永鋒、新宇、芳遠和澤遠'
复制代码

模块引入

### 以前
import * as utils from './utils.js';
export { utils };

### 现在
export * as utils from './utils.js';

复制代码

v8 与 nodejs 版本 Mapping
nodejs.org/zh-cn/downl…

如有语法遗漏,欢迎补充; 如有错误,欢迎指正。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值