chanjs 2.7.17 → 2.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/core/loader.js +20 -10
- package/package.json +1 -1
package/core/loader.js
CHANGED
|
@@ -60,17 +60,27 @@ export async function loadController(moduleName) {
|
|
|
60
60
|
if (!inst || typeof inst !== "object") continue;
|
|
61
61
|
|
|
62
62
|
// 原型方法批量bind,幂等标记避免重复绑定
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
Object.
|
|
70
|
-
|
|
63
|
+
// 遍历整条原型链(含父类继承的方法):子类方法定义在自身原型、父类模板方法定义在父类原型,
|
|
64
|
+
// 只 bind 直接原型会导致继承方法 this 丢失(Express 非点调用时 this=undefined)。
|
|
65
|
+
// ⚠️ 必须用 getOwnPropertyDescriptor 取 value:BaseComponent 的 app/config/db 是 getter-only,
|
|
66
|
+
// 其中 db 返回 knex 实例(typeof 为 function),直接 inst[key] 会触发 getter 且赋值只读属性抛错。
|
|
67
|
+
let proto = Object.getPrototypeOf(inst);
|
|
68
|
+
while (proto && proto !== Object.prototype) {
|
|
69
|
+
Object.getOwnPropertyNames(proto).forEach(key => {
|
|
70
|
+
if (key === "constructor") return;
|
|
71
|
+
const desc = Object.getOwnPropertyDescriptor(proto, key);
|
|
72
|
+
// 只 bind 数据方法(value 为函数);getter/setter 访问器跳过
|
|
73
|
+
if (!desc || typeof desc.value !== "function") return;
|
|
74
|
+
const fn = desc.value;
|
|
75
|
+
if (fn[BOUND_SYMBOL]) return;
|
|
76
|
+
const boundFn = fn.bind(inst);
|
|
77
|
+
Object.defineProperty(boundFn, BOUND_SYMBOL, {
|
|
78
|
+
value: true, enumerable: false, writable: false, configurable: false
|
|
79
|
+
});
|
|
80
|
+
inst[key] = boundFn;
|
|
71
81
|
});
|
|
72
|
-
|
|
73
|
-
}
|
|
82
|
+
proto = Object.getPrototypeOf(proto);
|
|
83
|
+
}
|
|
74
84
|
|
|
75
85
|
ctrlMap[ctrlName] = inst;
|
|
76
86
|
}
|