任何函数.constructor 永远 === Function
任何函数.constructor 永远 === Function
1. 底层原型链关系
所有普通函数(匿名函数、具名函数、箭头函数除外)都继承自
函数实例上的
Function.prototype。
constructor,来自原型链:fn.__proto__ === Function.prototype
Function.prototype.constructor === Function
所以:
任意普通函数.constructor 就是 原生 Function 构造器
2. 逐行拆解验证
// 随便定义一个函数
function fn() {}
// 1. 函数的 constructor
console.log(fn.constructor);
// → ƒ Function() { [native code] }
// 2. 和原生 Function 全等
console.log(fn.constructor === Function);
// → true
// 3. 和 Function.prototype.constructor 全等
console.log(fn.constructor === Function.prototype.constructor);
// → true
3. 常见写法等价
下面三个完全是同一个东西:
Function
Function.prototype.constructor
(function(){}).constructor
(()=>{}).constructor // 箭头函数也一样
4. 能干嘛?
可以绕过字面量限制,动态创建函数:
// 等价于 new Function("debugger")
const f = (function(){}).constructor("debugger;console.log('hi')");
f();
这就是逆向里常用的隐式构造函数创建,用来丢
debugger 反调试。5. 关键注意点
- 普通函数 / 箭头函数:
.constructor都是Function - 内置对象如 Array、String 实例:constructor 是自身构造器
[1,2].constructor === Array 'abc'.constructor === String

浙公网安备 33010602011771号