gpt4 book ai didi

node.js - Node.Js + Expressjs 中的继承

转载 作者:太空宇宙 更新时间:2023-11-03 22:02:59 24 4
gpt4 key购买 nike

我正在尝试在 Node.js 和 Express.js 中实现类似以下的目标,但无法找到有效的示例。感谢帮助。

base.js
--------
module.exports = {
baseFunction: function(){
.....
}
}

child.js
--------
module.exports = {
require('base'),

***** Some Magical Code ******

childFunction: function(){
.....
}
}



CallProgram.js
--------------
var child = require('child');
child.baseFunction();

最佳答案

这个怎么样?

function extend(a, b) {
var result = Object.create(a);
for (var prop in b) {
if (b.hasOwnProperty(prop)) {
result[prop] = b[prop];
}
}
return result;
}

module.exports = extend(require('base'), {
***** Some Magical Code ******

childFunction: function(){
.....
}
});

extend 函数将创建一个以 a 作为原型(prototype)的新对象,并将 b 的所有属性复制到其上。

关于node.js - Node.Js + Expressjs 中的继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12196723/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com