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的值赋给新的对象”。如何理解这句话?
js执行的过程中,又不是所有的代码都是js实现的,书上讲的是底层实现。
你现在把new的过程用call解释了,那你用什么js代码去解释call的实现呢?
(1)在内存中开辟了一块区域。
(2)this指向这块区域地址。
(3)操作这个区域。
(4)返回这个区域地址。