js中构造函数使用new创建对象时,this与新建对象关系是怎样的?

Slogic Slogic 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
JavaScript高级程序设计(第3版)P144
其中(2)所表达的意思。与我的理解有所出入。
-------------------------------引用高程内容 start---------------------------------------


英文解释
To create a new instance of Person, use the new operator. Calling a constructor in this manner
essentially causes the following four steps to be taken:

Create a new object.
Assign the this value of the constructor to the new object (so this points to the new object).
PS:(将构造函数的this的值赋给新的对象)

Execute the code inside the constructor (adds properties to the new object).
Return the new object.
------------------------------引用高程内容 end--------------------------------------
关于使用new关键字调用构造函数的步骤,以下是我的理解。
(1).var newobj = {}; //首先创建一个新的临时对象
(2).newobj.call(newobj ); //在新对象的作用域中执行构造函数。
也就是将newobj赋给this。而书上说的正相反。“将构造函数的this的值赋给新的对象”。如何理解这句话?
...全文

相关推荐

评论 3
收藏 已收藏
分享
返回顶部
Slogic
JavaScript 87,983 成员 224,680 内容