当前位置:网站首页>红宝书第四版的一个错误?
红宝书第四版的一个错误?
2022-07-27 00:14:00 【Wuuconix】
红宝书第四版的一个错误?
在 JavaScript高级程序设计(第四版)的 8.4节,那一节在介绍类,在第253页,pdf的话是在278页。

这里马特(红宝书第四版作者)想要说明的一个观点是,直接new 一个类,和new 类的构造函数 的结果是不一样的,然后它给出了一些代码,通过输出结果证明了确实不一样。
但是这里马特犯了一个错误,他试图通过Person.constructor获得类里面定义的constructor,但是实际上constructor是定义在Person.prototype上的,所以应该通过Person.prototype.constructor来获得这个构造函数。
以下是两者区别的直观显示。
class Person {
constructor() {
console.log("this is my constructor")
}
}
console.log(Person.prototype.constructor.toString())
console.log("--------")
console.log(Person.constructor.toString())
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YZuwMft4-1657434123957)(http://qiniu.wuuconix.link/image-20220710142003351.png)]](/img/2a/48952d1b2dd345f827781a94286633.png)
在把马特例子里获得构造器的代码更改后,我们可以获得以下结果。

输出的结果是一样的,所以直接调用 new Person(), 还是调用 new Person.prototype.construcor() 的结果都是一样的。
所以马特的观点被否决了。
实际上,利用 new Person() 在构造一个新实例的时候会调用constructor,所以两者的结果是不是一样,我们应该去关注Person.prototype和Person.prototype.constructor.prototype一不一样,因为这个属性决定了构造出来的实例的原型对象一不一样。
很显然是一样的,因为Person.prototype.constructor循环引用Person。
总结一下,马特作为大师,这里很可能只是一时糊涂,而我们作为初学者如何避免类似的错误呢?
我们需要明确,一个函数的prototype属性并不一定是这个函数的原型。具体文章可以见我的博文 论构造函数的原型是谁_Wuuconix的博客-CSDN博客
在今天这个例子中,class Person实际上也是一个函数,而马特误把 这个类的原型 想成了 Person.prototype,而它们实际上是不同的。
class Person {
}
console.log(Person.prototype === Object.getPrototypeOf(Person)) //false

这也是为什么我们不能通过Person.constructor获得到我们定义的构造器,因为Person这个类或者说函数的原型不是Person.prototype, 而我们写的这个构造函数是位于 Person.prototype上的。
边栏推荐
- LeetCode->二分查找打卡
- Talk about the metrics of automated testing
- iNFTnews | “流量+体验”白衬e数字时装节引领数字时装新变迁
- Uni app wechat applet search keywords are displayed in red
- Database read-write separation and database and table segmentation
- 小程序怎样助力智能家居生态新模式
- [redis] five common data types
- 想要彻底搞的性能优化,得先从底层逻辑开始了解~
- 白盒测试案例设计(我爷爷都能看懂)
- Shell 分析日志文件命令全面总结
猜你喜欢

Database knowledge required by testers: MySQL common syntax
![[nisactf 2022] upper](/img/61/05291ba7a63fe13882e49ab026df14.png)
[nisactf 2022] upper

LeetCode刷题——NO.238——除自身以外数组的乘积

次轮Okaleido Tiger即将登录Binance NFT,引发社区热议

想要彻底搞的性能优化,得先从底层逻辑开始了解~

Lua函数之非全局函数

MySQL master-slave database configuration based on docker for Ubuntu

「软件测试」包装简历从这几点出发,直接提升通过率

解决小程序报错getLocation:fail the api need to be declared in the requiredPrivateInfos field in app.json

小玩一个并行多线程MCU—MC3172
随机推荐
Pyqt5 use pyqtgraph to draw dynamic scatter chart
MySQL 5.7 takes the first item of the group
Arduino UNO +74HC164流水灯示例
Function stack frame explanation
Goatgui invites you to attend a machine learning seminar
Jmeter接口测试, 快速完成一个单接口请求
【Redis】五种常用的数据类型
Kubeadmin到底做了什么?
com.fasterxml.jackson.databind.exc.InvalidDefinitionException
信息收集-端口扫描工具-Nmap使用说明
As for the pit saved by serialized variables, the data added with indexer cannot be serialized
LeetCode->二分查找打卡
Greenplum【部署 08】数据库小版本升级流程及问题处理 Error: open-source-greenplum-db-6 conflicts with
C语言程序的编译上
Applet utils
Dynamically set the height of applet swiper
OD-Paper【3】:Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks
【RYU】安装RYU常见问题及解决办法
Cookie addition, deletion, modification and query methods
Cs224w fall course - --- 1.1 why graphs?