惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Help Net Security
Help Net Security
Recorded Future
Recorded Future
爱范儿
爱范儿
美团技术团队
博客园_首页
腾讯CDC
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
WordPress大学
WordPress大学
阮一峰的网络日志
阮一峰的网络日志
J
Java Code Geeks
T
Tailwind CSS Blog
雷峰网
雷峰网
V
V2EX
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
月光博客
月光博客
小众软件
小众软件
博客园 - 三生石上(FineUI控件)
人人都是产品经理
人人都是产品经理
酷 壳 – CoolShell
酷 壳 – CoolShell
L
LangChain Blog
量子位
Stack Overflow Blog
Stack Overflow Blog
Google DeepMind News
Google DeepMind News
博客园 - 司徒正美
V
Visual Studio Blog
罗磊的独立博客
Jina AI
Jina AI
宝玉的分享
宝玉的分享
C
CERT Recently Published Vulnerability Notes
Engineering at Meta
Engineering at Meta
S
SegmentFault 最新的问题
T
Threat Research - Cisco Blogs
F
Fortinet All Blogs
T
Tenable Blog
S
Securelist
D
DataBreaches.Net
P
Proofpoint News Feed
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Heimdal Security Blog
T
The Exploit Database - CXSecurity.com
S
Schneier on Security
Martin Fowler
Martin Fowler
W
WeLiveSecurity
MyScale Blog
MyScale Blog
The Cloudflare Blog
Security Latest
Security Latest
Hugging Face - Blog
Hugging Face - Blog
Scott Helme
Scott Helme
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cloudbric
Cloudbric

JieJiSS' Blog

为 SQLAlchemy Model 添加 type hint 和 type check Boot an Arch Linux RISC-V using qemu-system 关于基于机器学习的多目标对象追踪算法的文献综述 - JieJiSS' Blog RV64 板子更换 rootfs 指南 - JieJiSS' Blog ACTF2022 safer-tg-bot-{1,2} WP - JieJiSS' Blog A RISC-V gcc pitfall revealed by a glibc update Setup an Arch Linux RISC-V Development Environment 解构赋值踩坑:神秘失踪的数据 - JieJiSS' Blog How's the GDPR (used to) related to my blog Rust is incompatible with LLVM, at least partially python-mtrpacket 在 riscv64 上编译测试不通过 - JieJiSS' Blog 快速获得 RISC-V 开发环境 - JieJiSS' Blog 在 RISC-V 上编译 Node.js 16 Redmi AirDots 3 Pro 连接电脑后蓝牙类别不正确的解决方案 Bilibili 2020-10-24 CTF WriteUp - JieJiSS' Blog 对于又一种对SS流式加密的攻击(奇虎团队)的详细分析 - JieJiSS' Blog 在MacOS上配置dnscrypt-proxy(免分流) - JieJiSS' Blog 市县中心点坐标(经纬度) - JieJiSS' Blog 二项式定理及其推广公式的一种简单理解办法 - JieJiSS' Blog
为什么 ArrowFunction 不能 new - JieJiSS' Blog
2020-10-29 · via JieJiSS' Blog

0x00 TL;DR

因为 Lexical this 导致没有 [[Construct]],所以不能 new

0x01 详细版本

如果去查一下 ECMA spec,其实可以看到如下的解释:

A function object is an object that supports the [[Call]] internal method. A constructor is an object that supports the [[Construct]] internal method. Every object that supports [[Construct]] must support [[Call]]; that is, every constructor must be a function object. Therefore, a constructor may also be referred to as a constructor function or constructor function object.

所以想要对某个对象使用 new,就得确保该对象具有 [[Construct]] 这个内部方法。而 ArrowFunction 没有 [[Construct]]

再多查一点的话,可以在 14.2.17 这一节看到如下的注解:

An ArrowFunction does not define local bindings for arguments, super, this, or new.target. Any reference to arguments, super, this, or new.target within an ArrowFunction must resolve to a binding in a lexically enclosing environment.

再多查一点的话,可以找到最终通过的 proposal:https://tc39wiki.calculist.org/es6/arrow-functions/

The goal of Arrow Functions is to address and resolve several common pain points of traditional Function Expression:

  • Lexical this binding;
  • Shorter syntactical form (() => {} vs. function () {})

Lexical this matches the dominant cohort measured by Kevin Smith (see the BTF Measurements thread that either does not use this or wants it lexically bound.

  • Best results after modifying subject code to use method definition shorthand and only then scanning for function.
  • From lexical this it follows that arrow functions are not constructors (no .prototype or [[Construct]]).

再多查一点的话,你可以在 2011 年的一次讨论里找到这个设计的来源:https://mail.mozilla.org/pipermail/es-discuss/2012-March/021953.html

We do not want to delegate to a target function that can [[Construct]], because there is no such target function -- we're not trying to sugar .bind on a full function. That is too expensive and general (no pre-args, no full function under the hood).

Lexical-only this binding means no [[Construct]] and no .prototype for arrow functions. I'm ok with this.


考虑典型的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class A {
constructor() {
this.attr = 0;
}
}

const a = new A();



function A() {
this.attr = 0;
}

const a = new A();

在这两种构造实例的方法中,this 指向的都是正在被操作的变量 a。 但是 arrow function 在还是个草案的时候就只计划支持 Lexical this,因此它的函数体中的 this 不会指向正在被操作的变量 a,无法修改赋值表达式的左值,因此就算 ArrowFunction[[Construct]] 内部方法也无法用于构造新对象。

来源:https://blog.jiejiss.com/