构造函数显式返回引用类型对象时,new 表达式直接返回该对象,原实例被丢弃,导致原型链断裂、instanceof 失效、constructor 属性丢失;返回原始值或 null 则忽略 return,仍返回新实例。

如果构造函数显式返回一个对象(包括数组、函数、普通对象等引用类型),new 表达式会直接返回这个对象,而不是默认创建的那个新实例。此时,原本由 new 创建的对象被丢弃,原型链、this 绑定、constructor 属性等都与返回的对象无关。
返回对象时,原实例完全失效
构造函数内部 this 指向的是 new 创建的新对象,但一旦 return 了一个引用值,这个新对象就不再被返回——它甚至不会出现在最终结果中。
- 返回的对象不会继承构造函数的 prototype
- 该对象的 __proto__ 不指向构造函数的 prototype
- 该对象没有 constructor 属性指向原构造函数
- 原型上的方法(如 Person.prototype.sayHello)无法在该对象上调用
典型例子对比
下面两个构造函数仅差一行 return,行为却完全不同:
function Person(name) {
this.name = name;
return { name: 'override', age: 99 }; // 返回对象
}
const p1 = new Person('Alice');
console.log(p1); // { name: 'override', age: 99 }
console.log(p1 instanceof Person); // false
console.log(p1.constructor === Person); // false
而如果没有 return 或返回原始值:
function Person(name) {
this.name = name;
return 'ignored'; // 字符串,原始值 → 被忽略
}
const p2 = new Person('Bob');
console.log(p2); // Person { name: 'Bob' }
console.log(p2 instanceof Person); // true
console.log(p2.constructor === Person); // true
哪些值算“对象”?
只要 return 的值是引用类型,就会触发覆盖行为:
- 普通对象:return { x: 1 }
- 数组:return [1, 2, 3]
- 函数:return function() {}
- 正则、日期、Map、Set 等内置对象:return new Date()
- null 是例外:return null 不算对象,仍返回新实例(因为 null 是原始值)
实际开发中要注意什么?
这种返回机制容易引发隐性 bug:
- 忘记注释或误加 return {},导致实例丢失原型方法
- 在构造函数中做条件返回(如缓存命中时 return cachedObj),需确保返回对象具备必要接口
- ES6 class 的 constructor 不允许 return 非 this 对象,否则报错;但 function 构造函数仍允许











