til
  • README
  • Software Development Roles
  • solid
  • README
    • service-worker
  • docker
    • arg
    • 更新docker版本
  • editor
    • vscode
    • Creating a VS Code Theme
  • english
    • words
  • front-end
    • ==
    • ECMAScript
    • IIFE
    • Label
    • basic
    • html.js.css渲染顺序
    • npm-vs-yarn
    • obj-delete-key-value
    • react
    • split-join-and-replace
    • video
    • 前端自检清单
    • 递归及去重
    • css
      • css换肤
      • flex
      • list
      • nth-child和nth-of-type区别
      • padding
      • position
      • 层叠上下文
      • 层叠样式(+)
      • 正方形
      • 语义化标签
    • dom
      • DOCTYPE
      • HEAD
      • 修改document
      • 自定义表单验证
    • electron
      • basic
    • es6
      • basic-type
      • basic
      • prototype-example
      • defineProperty
      • understanding-es6
        • 0.introduction
        • Appendix A: Smaller Changes
        • Appendix B: Understanding ES7
        • Block-Binding
        • Proxies&Reflection
        • class
        • 解构赋值
        • function
        • improved-array
        • iterators&generators
        • modules
        • object
        • promise
        • Map&Set
        • symbol
    • images
      • 前端角度看图片
    • interview_case
      • lexical_scope
      • redux和localstroage存储位置
    • javascript
      • fuck-the-js
      • js-engine-work
      • js原生操作dom
      • what-is-function-program
      • 执行上下文
      • articles
        • JavaScript中使用函数组合
        • JavaScript中的依赖注入
        • JavaScript作用域链中的标识符解析和闭包
        • JavaScript是何如工作的--概述
        • JavaScript深拷贝
        • JavaScript的全局变量是如何工作的
        • js继承常见的误解
        • node12&chrome中7个新的提案功能
        • 你真的懂JavaScript吗
      • date
        • index
      • engines
        • basic
        • JavaScript引擎基础:外形和内联缓存
        • v8中推测性优化的介绍
        • 优化原型
        • 更快的异步功能和promise
      • events
        • baisc
        • 事件冒泡和捕获
        • 定义事件
        • 页面生命周期
      • higher-order-function
        • curry
        • monad
      • module
        • basic
        • main&module
      • objects
        • iterator
        • spread
        • examples
          • iterator
      • performance
        • blocking-css
        • cache
      • prototype
        • Property-Descriptors
        • basic
        • prototype-shadow
      • you-dont-known-js
        • async&performance
          • Chapter 1: Asynchrony: Now & Later
          • Chapter 2: Callbacks
          • Chapter 3: Promises
          • Chapter 4: Generators
        • scope & closures
          • apA
          • apB
          • apC
          • apD
          • chapter1-what-is-scope
          • chapter2-lexical-scope
          • chapter3-function-vs-block-scope
          • chapter4-hoisting
          • chapter5-scope-closure
        • this & object prototypes
          • chapter1-this-or-that
          • chapter2-this-make-sense
          • chapter3-objects
          • chapter4-mixing(up)-class-object
          • chapter5-prototype
          • chapter6-behavior-delegation
        • types&grammer
          • Chapter1-Types
          • Chapter2-Values
          • Chapter3-Natives
          • Chapter4-coercion
          • Chapter5-grammer
        • up & going
          • chapter1-into-programming
          • chapter2-into-javascript
          • chapter3-into-YDKJS
    • mobile
      • iPhone分辨率终极指南
    • npm
      • arguments
      • build
    • react-native
      • prop-methods
    • react
      • PropTypes
      • basic
      • codebase-overview
      • component-element-instance
      • context
      • how-to-known-component-is-func-or-class
      • overview
      • react16.9
      • react18计划
      • react的设计原则
      • reconciliation
      • setState
      • useMemo
      • why-do-we-write-super-props
      • 从头实现一个react
      • concurrent
        • 引入并发模式(仅试验)
      • conf
        • conf-2019
      • events
        • 合成事件概述
      • hooks
        • custom-hook
        • effect-hook
        • hooks-api
        • intro
        • overview
        • rules
        • state-hook
        • hooks-vs-class
          • thinking-in-react-hooks
      • overreact
        • Development模式是如何工作的
        • How-Does-setState-Know-What-to-Do
        • Why-Do-React-Elements-Have-a-$$typeof-Property
        • Why-Do-React-Hooks-Rely-on-Call-Order
        • how-to-known-component-is-func-or-class
        • preparing-tach-talk-motivation
        • react作为ui运行
        • things-i-dont-known-as-2018
        • ui-element-problem-and-build-yourself
        • why-do-we-write-super-props
        • 一份完整的useEffect指南
        • 为什么X不是Hook
        • 函数组件与类有什么不同?
        • 演讲准备2-what-why-how
        • 编写弹性组件
        • 让setInterval在React-Hooks中为声明式
      • practice
        • render
      • react-dom
        • basic
      • react-redux
        • apiv7.1-hooks
        • connect
        • shallow-equal
      • redux
        • applyMiddleware
        • applyMiddleware2-细节
        • example
    • regex
      • index
    • stories
      • 数组下标
      • 阻止事件冒泡
    • svelte
      • compile-svelte-in-your-head-1
      • compiler-overview
      • parser
        • 写一个解析器-JavaScript的JSON解析器
    • turbopack
      • basic
    • typescript
      • interface和type的区别
    • webpack
      • hash
      • webpack4-for-react
      • webpack4
      • webpack4to5
      • babel
        • babel-parser和acorn的区别
        • babel.7.11
        • family
        • react16.14使用new-transform
        • update-to-7
    • pdf
      • deep-js
        • basic
      • react
        • reintroducing
  • git
    • capital
    • emoji
  • http
    • http2.0
    • response
  • rails
    • api
    • flash
    • middleware-vs-metal
    • model
    • performance
    • routes
    • environment
      • error
    • patterns
      • service
    • sidekiq
      • params
    • deploy
      • capistrano
        • ssh
  • ruby
    • self
    • net
      • http请求携带cookie
  • server
    • ss
    • ssh
    • user
    • crawler
      • puppeteer
    • nginx
      • domain-without-80
      • nginx节省带宽
  • sql
    • rails
    • search
Powered by GitBook
On this page
  • 1. 私有字段 #
  • 2. 可选的链?.
  • 3. null合并 ??
  • 4. BigInt 1n
  • 5. static 字段
  • 6. 顶层 await
  • 7. WeakRef
  1. front-end
  2. javascript
  3. articles

node12&chrome中7个新的提案功能

Previousjs继承常见的误解Next你真的懂JavaScript吗

Last updated 5 years ago

JavaScript(或ECMA script)是一种不断发展的语言,有许多关于如何更好发展的建议和想法。TC39(技术委员会39)是负责定义JS标准和特性的委员会,今年他们一直很活跃。以下是目前处于"第3阶段"的一些提案的摘要,这是在"完成"之前的最后一个阶段。这意味着这些功能应该很快就会在浏览器和其他引擎中实现。事实上,其中一些现在可用。

1. 私有字段 #

Chrome & NodeJS 12 中可用

是的,你没有看过。最终,JS在类中获取私有字段。不再需要this._doPrivateStuff(),定义闭包来存储私有值,或者使用WeakMap来hack私有props。

don't touch my garbage

语法看起来像下面这样:

// 私有字段必须使用'#'开头
// 并且不可以在类这个块的外面去访问

class Counter {
  #x = 0;

  #increment() {
    this.#x++;
  }

  onClick() {
    this.#increment();
  }

}

const c = new Counter();
c.onClick(); // works fine
c.#increment(); // error

2. 可选的链?.

以前必须访问嵌套在对象内的几个级别的属性,并得到臭名昭著的错误Cannot read property 'stop' of undefined。然后更改代码以处理链中的每个可能是undefined的对象,例如:

const stop = please && please.make && please.make.it && please.make.it.stop;

// 或可以使用像'object-path'这样的库
const stop = objectPath.get(please, "make.it.stop");

通过可选的链接,你就可以像下面这样来处理:

const stop = please?.make?.it?.stop;

3. null合并 ??

变量的可选值可能缺失,如果丢失,则使用默认值,这样属于很常见的现象:

const duration = input.duration || 500;

键入null合并运算符,他只会覆盖undefined或null:

const duration = input.duration ?? 500;

4. BigInt 1n

Chrome & NodeJS 12 中可用

JS一直不擅长数学的一个原因是我们无法可靠地存储大于2 ^ 53的数,这使得处理相当大的数非常困难。幸运的是,BigInt是一个解决这个特定问题的提案。

闲话少说,show me your code:

// 可以定义 BigInt 通过追加 'n' 到一个数字字面量
const theBiggestInt = 9007199254740991n;

// 对字面量使用构造函数
const alsoHuge = BigInt(9007199254740991);

// 或者对字符串使用
const hugeButString = BigInt('9007199254740991');

你还可以在BigInt上使用与普通数字相同的运算符,例如:+,-,/,*,%,…不过有一个问题,在大多数操作中不能将BigInt与数字混合使用。比较Number和BigInt工作方式,但不能添加他们:

1n < 2 
// true

1n + 2
// 🤷‍♀️ Uncaught TypeError: Cannot mix BigInt and other types, use explicit conversions

译:根据上面的报错提示,你可以改为显示的转换

Number(1n) + 2

5. static 字段

Chrome & NodeJS 12 中可用

这个很简单。它允许在类上有一个静态字段,类似于大多数OOP语言。静态字段可以作为枚举的替代,它们也可以用于私有字段。

class Colors {
  // public static fields
  static red = '#ff0000';
  static green = '#00ff00';

  // private static fields
  static #secretColor = '#f0f0f0';

}


font.color = Colors.red;

font.color = Colors.#secretColor; // Error

6. 顶层 await

chrome中可用

允许在你代码的顶层使用await。这对于调试浏览器控制台中的异步内容(如fetch)非常有用,而无需将其封装在async函数中。

另一个致命的用例是,它可以在以异步方式初始化的ES模块的顶层使用(比如建立连接的数据库层)。当import这样的“异步模块”时,模块系统将在执行依赖它的模块之前等待它解析。这将使处理异步初始化比当前返回初始化promise并等待它的工作区容易得多。模块将不知道它的依赖关系是否是异步的。

// db.mjs
export const connection = await createConnection();
// server.mjs
import { connection } from './db.mjs';

server.start();

在此示例中,在db.mjs中完成连接之前,不会在server.mjs中执行任何操作。

7. WeakRef

Chrome & NodeJS 12 中可用

对象的弱引用是不再能够使对象保持活的引用。每当我们使用(const、let、var)创建一个变量时,只要该变量的引用仍然可访问,垃圾收集器(GC)就永远不会从内存中删除该变量。这些都是强引用。但是,如果没有对弱引用引用的对象有强引用,则GC可以在任何时候删除它。WeakRef实例有一个deref方法,它返回被引用的原始对象,如果原始对象被垃圾回收期收集,则返回undefined。

这对于缓存廉价对象可能很有用,因为你不想将所有对象都永远存储在内存中。

const cache = new Map();

const setValue =  (key, obj) => {
  cache.set(key, new WeakRef(obj));
};

const getValue = (key) => {
  const ref = cache.get(key);
  if (ref) {
    return ref.deref();
  }
};

// this will look for the value in the cache
// and recalculate if it's missing
const fibonacciCached = (number) => {
  const cached = getValue(number);
  if (cached) return cached;
  const sum = calculateFibonacci(number);
  setValue(number, sum);
  return sum;
};

对于缓存远程数据来说,这可能不是一个好主意,因为远程数据从内存中删除 不可预测。在这种情况下,最好使用类似LRU缓存的东西。

提案:

提案:

||的问题是它将覆盖值,如(0, '', false),在某些情况下可能是有效的输入。

提案:

此图片被和谐

提案:

提案:

using await in browser console

如果你需要复习异步并等待,。

wait for it

提案:

译:LRU缓存,是否有去了解?或者下次一起了解下

提案:

就是这样。我希望你和我一样会兴奋地使用这些很酷的新功能。有关这些提案以及我未提及的其他提案的更多详细信息,。

原文:

https://github.com/tc39/proposal-class-fields
https://github.com/tc39/proposal-optional-chaining
所有falsy
https://github.com/tc39/proposal-nullish-coalescing
https://github.com/tc39/proposal-bigint
https://github.com/tc39/proposal-static-class-features
请看我在这篇文章里对他的解释
https://github.com/tc39/proposal-top-level-await
Implementing LRU cache in JavaScript -- medium
LRU cache implementation in Javascript -- stackoverflow
https://github.com/tc39/proposal-weakrefs
请保持关注github上的TC39提案
7 Exciting New JavaScript Features You Need to Know