函数表现
1 | function F1() { |
运行结果 👇
1 | 1: f1 |
引用 MDN 的一段话关于 new 操作符
The new operator lets developers create an instance of a user-defined object type or of one of the built-in object types that has a constructor function. The new keyword does the following things:
- Creates a blank, plain JavaScript object;
- Links (sets the constructor of) this object to another object;
- Passes the newly created object from Step 1 as the this context;
- Returns this if the function doesn’t return its own object.
new
做了这 4 件事
- 创建一个空的简单 JavaScript 对象(即{})
- 链接该对象(即设置该对象的构造函数)到另一个对象
- 将步骤 1 新创建的对象作为 this 的上下文
- 如果该函数没有返回对象,则返回 this
函数执行默认返回 undefinednew
构造函数默认返回this
指定返回值优先级最高